我有Test, Question, Testquestion
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
创建Test
时,我希望传递order
列Testquestion
的值。
def create
Test.create(test_params)
end
def test_params
params.require(:test).permit(:testname,question_attributes:[:questionname])
end
我应该如何传递order
列的值,以便关联模型(Testquestion)得到更新。
答案 0 :(得分:2)
没有办法这样做,你需要走更长的路。
accepts_nested_attributes_for
添加到您的对话中,以便嵌套操作能够正常运行。 accepts_nested_attributes_for :question
添加到Testquestion
。这样的事情:
{
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。旁注:您应该真正获得在snake_case
和CamelCase
中的课程中命名字段和变量的习惯。