Rails:多对多关系 - 创建和关联新记录
Rails: many to many relationship - creating and associating new records
我将此 railscast #17 revised 视为使用连接在两个模型之间创建多对多关系的教程 table:
class Course < ActiveRecord::Base
has_many :courselocalizations
has_many :courselocations, through: :courselocalizations
end
class Courselocation < ActiveRecord::Base
has_many :courselocalizations
has_many :courses, through: :courselocalizations
end
class Courselocalization < ActiveRecord::Base
belongs_to :course
belongs_to :courselocation
end
如果用户在编辑或创建 course
时没有看到现有的 courselocation
,他们创建新 courselocation
并自动建立关联的最佳方式是什么?
您可以分两步完成(我的首选方法),也可以一步完成。
2步过程:
- 创建 CourseLocation
- 关联 CourseLocation
和 Course
。
course_location1 = CourseLocation.create(name: 'location1')
course.course_locations << course_location1
如果您想一步完成,可以使用 Active Record accepts_nested_attributes_for
method, this is a little tricky. Here is a good article,但让我们分解这些步骤:
- 课程需要接受 course_localizations
的属性
- 课程本地化需要接受 course_location
的属性
- 创建课程时,传递包含位置属性的本地化属性。
让我们针对您的特定情况进行设置:
class Course < ActiveRecord::Base
# attribute subject
has_many :course_localizations, inverse_of: :course
has_many :course_locations, through: :course_localizations
accepts_nested_attributes_for :course_localizations
end
class CourseLocation < ActiveRecord::Base
# attribute city
has_many :course_localizations
has_many :courses, through: :course_localizations
end
class CourseLocalization < ActiveRecord::Base
belongs_to :course
belongs_to :course_location
accepts_nested_attributes_for :course_locations
end
此时你应该可以
course = Course.new(
subject: "Math",
course_localizations_attributes: [
{ course_location_attributes: { city: "Los Angeles" } },
{ course_location_attributes: { city: "San Francisco" } }]
)
course.save
我将此 railscast #17 revised 视为使用连接在两个模型之间创建多对多关系的教程 table:
class Course < ActiveRecord::Base
has_many :courselocalizations
has_many :courselocations, through: :courselocalizations
end
class Courselocation < ActiveRecord::Base
has_many :courselocalizations
has_many :courses, through: :courselocalizations
end
class Courselocalization < ActiveRecord::Base
belongs_to :course
belongs_to :courselocation
end
如果用户在编辑或创建 course
时没有看到现有的 courselocation
,他们创建新 courselocation
并自动建立关联的最佳方式是什么?
您可以分两步完成(我的首选方法),也可以一步完成。
2步过程:
- 创建 CourseLocation
- 关联 CourseLocation
和 Course
。
course_location1 = CourseLocation.create(name: 'location1')
course.course_locations << course_location1
如果您想一步完成,可以使用 Active Record accepts_nested_attributes_for
method, this is a little tricky. Here is a good article,但让我们分解这些步骤:
- 课程需要接受 course_localizations 的属性
- 课程本地化需要接受 course_location 的属性
- 创建课程时,传递包含位置属性的本地化属性。
让我们针对您的特定情况进行设置:
class Course < ActiveRecord::Base
# attribute subject
has_many :course_localizations, inverse_of: :course
has_many :course_locations, through: :course_localizations
accepts_nested_attributes_for :course_localizations
end
class CourseLocation < ActiveRecord::Base
# attribute city
has_many :course_localizations
has_many :courses, through: :course_localizations
end
class CourseLocalization < ActiveRecord::Base
belongs_to :course
belongs_to :course_location
accepts_nested_attributes_for :course_locations
end
此时你应该可以
course = Course.new(
subject: "Math",
course_localizations_attributes: [
{ course_location_attributes: { city: "Los Angeles" } },
{ course_location_attributes: { city: "San Francisco" } }]
)
course.save