【发布时间】:2014-01-07 12:33:44
【问题描述】:
我有以下型号
class Course < ActiveRecord::Base
has_many :syllabuses, dependent: :destroy
has_many :topics, through: :syllabuses
end
class Topic < ActiveRecord::Base
has_many :syllabuses, dependent: :destroy
has_many :courses, through: :syllabuses
end
class Syllabus < ActiveRecord::Base
belongs_to :course
belongs_to :topic
end
我在routes 中使用topic 嵌套在course 中
现在在保存新的topic 时,以下代码可以工作:-
@course = Course.find(params[:course_id])
@topic = @course.topics.create(topic_params)
这不是
@course = Course.find(params[:course_id])
@topic = @course.topics.new(topic_params)
@topic.save
新的topic 已保存,但syllabus 中没有保存任何关联,我想我可能需要在这里使用build 功能,有人可以帮忙吗?
【问题讨论】:
标签: ruby-on-rails activerecord