Caffeine Coder

Jul 8, 2017 09:57
:-)
Jul 8, 2017 09:53
Thanks again
Jul 8, 2017 09:52
Had u not helped , this wudnt had gone anywhere and maybe wud had taken a day more to me
Jul 8, 2017 09:50
Lesson learnt , need to look into this inverse_of
Jul 8, 2017 09:49
and data is coming into db
Jul 8, 2017 09:49
finally
Jul 8, 2017 09:49
IT GOT CREATED
Jul 8, 2017 09:48
ok
Jul 8, 2017 09:47
Could not find the inverse association for answers (:questions in Answer)
Jul 8, 2017 09:46
class Question < ApplicationRecord
# attr_accessor :content, :survey_id, :answers_attributes
belongs_to :survey, inverse_of: :questions
has_many :answers, inverse_of: :questions
accepts_nested_attributes_for :answers, allow_destroy: true
end
Jul 8, 2017 09:43
so i think answer will have inverse_of question
Jul 8, 2017 09:43
This is what i get - Could not find the inverse association for answers (:questions in Answer)
Jul 8, 2017 09:42
class Survey < ApplicationRecord
# attr_accessor :name, :questions_attributes
has_many :questions, inverse_of: :survey
accepts_nested_attributes_for :questions, allow_destroy: true
end
Jul 8, 2017 09:42
class Answer < ActiveRecord::Base
# attr_accessor :content, :question_id
belongs_to :question, inverse_of: :answers
end
Jul 8, 2017 09:42
class Question < ApplicationRecord
# attr_accessor :content, :survey_id, :answers_attributes
belongs_to :survey
has_many :answers, inverse_of: :questions
accepts_nested_attributes_for :answers, allow_destroy: true
end
Jul 8, 2017 09:39
this is hoq questions looks like now
Jul 8, 2017 09:39
did that
Jul 8, 2017 09:37
i wrote inverse_of in survey and question
Jul 8, 2017 09:37
answer.rb shudnt have any inverse , right ?
Jul 8, 2017 09:33
no , let me do that
Jul 8, 2017 09:32
so questions can be created only when they have a survey
Jul 8, 2017 09:31
But questions.build is called on survey
Jul 8, 2017 09:30
2.3.1 :017 > survey = Survey.new({
2.3.1 :018 > name: "pop",
2.3.1 :019 > questions_attributes: [{ content: "First question" }]
2.3.1 :020?> })
=> #<Survey id: nil, name: "pop", created_at: nil, updated_at: nil>
2.3.1 :021 >
2.3.1 :022 > survey.valid?
=> false
2.3.1 :023 > survey.errors
=> #<ActiveModel::Errors:0x000000067f6790 @base=#<Survey id: nil, name: "pop", created_at: nil, updated_at: nil>, @messages={:"questions.survey"=>["must exist"]}, @details={:"questions.survey"=>[{:error=>:blank}]}>
Jul 8, 2017 09:28
so they cant be created without a survey id
Jul 8, 2017 09:28
questions and answers are tied to survey
Jul 8, 2017 09:27
2.3.1 :016 > question = Question.new;question.valid?;question.errors
=> #<ActiveModel::Errors:0x00000009298278 @base=#<Question id: nil, survey_id: nil, integer: nil, content: nil, created_at: nil, updated_at: nil>, @messages={:survey=>["must exist"]}, @details={:survey=>[{:error=>:blank}]}>
Jul 8, 2017 09:27
I understand , no worries
Jul 8, 2017 09:26
question = Question.new;question.valid? question.errors
=> false
Jul 8, 2017 09:25
Question.new
=> #<Question id: nil, survey_id: nil, integer: nil, content: nil, created_at: nil, updated_at: nil>
Jul 8, 2017 09:24
i think these have issues
Jul 8, 2017 09:23
Answer.create
(0.1ms) BEGIN
(0.1ms) ROLLBACK
=> #<Answer id: nil, question_id: nil, content: nil, created_at: nil, updated_at: nil>
2.3.1 :013 > Question.create
(0.1ms) BEGIN
(0.1ms) ROLLBACK
=> #<Question id: nil, survey_id: nil, integer: nil, content: nil, created_at: nil, updated_at: nil>
2.3.1 :014 >
Jul 8, 2017 09:23
Questions.create
NameError: uninitialized constant Questions
Jul 8, 2017 09:22
nope
Jul 8, 2017 09:20
Survey.create({
2.3.1 :005 > name: "pop",
2.3.1 :006 > questions_attributes: [
2.3.1 :007 > { content: "First question" }
2.3.1 :008?> ]
2.3.1 :009?> })
(0.1ms) BEGIN
(0.1ms) ROLLBACK
=> #<Survey id: nil, name: "pop", created_at: nil, updated_at: nil>
2.3.1 :010 >
Jul 8, 2017 09:18
Survey.create
(0.1ms) BEGIN
SQL (0.2ms) INSERT INTO `surveys` (`created_at`, `updated_at`) VALUES ('2017-07-08 09:18:32', '2017-07-08 09:18:32')
(41.0ms) COMMIT
=> #<Survey id: 11, name: nil, created_at: "2017-07-08 09:18:32", updated_at: "2017-07-08 09:18:32">
Jul 8, 2017 09:18
?
Jul 8, 2017 09:18
Survey.create
Jul 8, 2017 09:17
there
Jul 8, 2017 09:14
nope , nothing
Jul 8, 2017 09:13
class SurveysController < ApplicationController
before_action :set_survey, only: [:show, :edit, :update, :destroy]

# GET /surveys
# GET /surveys.json
def index
@surveys = Survey.all
end

# GET /surveys/1
# GET /surveys/1.json
def show
end

# GET /surveys/new
def new
@survey = Survey.new
3.times do
question = @survey.questions.build
4.times { question.answers.build }
end
end

# GET /surveys/1/edit
def edit
end

# POST /surveys
# POST /surveys.json
def create
@survey = Survey.new(survey_params)
# @form = @survey.questions.create(params[:survey_params])
Jul 8, 2017 09:13
Let me post the whole survey controller
Jul 8, 2017 09:13
Nope , that's gone
Jul 8, 2017 09:13
def create
@survey = Survey.new(survey_params)
# @form = @survey.questions.create(params[:survey_params])

respond_to do |format|
if @survey.save
format.html { redirect_to @survey, notice: 'Survey was successfully created.' }
format.json { render :show, status: :created, location: @survey }
else
format.html { render :new }
format.json { render json: @survey.errors, status: :unprocessable_entity }
end
end
end
Jul 8, 2017 09:12
rollback
Jul 8, 2017 09:12
still the same
Jul 8, 2017 09:08
is it because i added id to it ?
Jul 8, 2017 09:08
Processing by SurveysController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"GkowBuIX97A98Koj0ZpDrB3Mj3RaHAh4MemjQ5wAwre6/iaTLf9z0YPv8pbPTzo8LYlC0/jS7CGEeJNSz7Tdcw==", "survey"=>{"name"=>"pop", "questions_attributes"=>{"0"=>{"content"=>"dagdfg", "_destroy"=>"0", "answers_attributes"=>{"0"=>{"content"=>"gggggggggg", "_destroy"=>"0"}, "1"=>{"content"=>"ffffffffff", "_destroy"=>"0"}, "2"=>{"content"=>"dddddddddddddd", "_destroy"=>"0"}, "3"=>{"content"=>"ssssssssssss", "_destroy"=>"0"}}}, "1"=>{"content"=>"werwerwer", "_destroy"=>"0", "answers_attributes"=>{"0"=>{"content"=>"
Jul 8, 2017 09:05
the survey table takes in data though
Jul 8, 2017 09:05
the question and answer tables are blank
Jul 8, 2017 09:04
just never got inserted