Reputation: 118
I have a class Test, Question, Testquestion
associated by has_many through
class Test
has_many :testquestions
has_many :questions, through: :testquestions
end
class Question
has_many :testquestions
has_many :tests, through: :testquestions
end
class Testquestion
belongs_to :test
belongs_to :questions
end
While creating a Test
i want to pass a value of column order
of Testquestion
.
def create
Test.create(test_params)
end
def test_params
params.require(:test).permit(:testname,question_attributes:[:questionname])
end
How should i pass value of order
column so that associated model(Testquestion) get updated.
Upvotes: 1
Views: 742
Reputation: 4147
There's no way to do it that way, you will need to take a longer path.
accepts_nested_attributes_for
to your assossiations, so that nested operations would actualy work. accepts_nested_attributes_for :question
to Testquestion
.Something like this:
{
test: {
testname: 'someName',
testquestion_attributes: {
order: someOrder,
question_attributes: {
questionname: 'someName'
}
}
}
}
params.require(:test).permit(:testname, testquestion_params: [:id, :order, :_destroy, question_params: [:id, :questionname])
p.s. sidenote: you should really aquire a habbit of naming your fields and variables in snake_case
and your classes in CamelCase
.
Upvotes: 2