Here is my scenario:
这是我的情景:
A model called Course has many CourseCodes. A CourseCode belongs to a Course. A CourseCode can't be created without Course and a Course can't be created without at least one CourseCode.
名为Course的模型有许多CourseCodes。 CourseCode属于一个课程。如果没有课程,则无法创建CourseCode,并且如果没有至少一个CourseCode,则无法创建课程。
class Course < ActiveRecord::Base
has_many :course_codes
validate :existence_of_code
private
def existence_of_code
unless course_codes.any?
errors[:course_codes] << "missing course code"
end
end
end
class CourseCode < ActiveRecord::Base
belongs_to :course
validates_presence_of :course
end
The whole scenario feels a bit like catch 22. Is there a way to create both on the same time? I'm using Rails 3.2
整个场景感觉有点像捕获22.有没有办法在同一时间创建两者?我正在使用Rails 3.2
3 个解决方案
#1
1
Solved the problem by using accepts_nested_attributes_for.
使用accepts_nested_attributes_for解决了这个问题。
Nested attributes allow you to save attributes on associated records through the parent. By default nested.
嵌套属性允许您通过父级保存关联记录的属性。默认情况下嵌套。
class Course < ActiveRecord::Base
has_many :course_codes, inverse_of: :course
validate :existence_of_code
accepts_nested_attributes_for :course_codes
private
def existence_of_code
unless course_codes.any?
errors[:course_codes] << "missing course code"
end
end
end
class CourseCode < ActiveRecord::Base
belongs_to :course, inverse_of: :course_codes
validates_presence_of :course
end
Used like this.
像这样使用。
Course.create!({
course_codes_attributes: [{ code: "TDA123" }],
# ...
})
#2
-1
Looks good to me. Removing the validates_presence_of :course
might make things easier on you, too, as it will tend to get in the way an not add much.
在我看来很好。删除validates_presence_of:course也可能会让事情变得更容易,因为它往往会妨碍不添加更多内容。
When you create a course, do it like this:
创建课程时,请执行以下操作:
Course.create course_codes: [CourseCode.new(...), CourseCode.new(...)]
ActiveRecord will figure things out.
ActiveRecord会解决问题。
#3
-1
You could add an unless
to whichever model you would plan to create first. For instance:
您可以添加一个除非您计划首先创建的模型。例如:
class CourseCode < ActiveRecord::Base
belongs_to :course
validates_presence_of :course, :unless => lambda { Course.all.empty? }
end
#1
1
Solved the problem by using accepts_nested_attributes_for.
使用accepts_nested_attributes_for解决了这个问题。
Nested attributes allow you to save attributes on associated records through the parent. By default nested.
嵌套属性允许您通过父级保存关联记录的属性。默认情况下嵌套。
class Course < ActiveRecord::Base
has_many :course_codes, inverse_of: :course
validate :existence_of_code
accepts_nested_attributes_for :course_codes
private
def existence_of_code
unless course_codes.any?
errors[:course_codes] << "missing course code"
end
end
end
class CourseCode < ActiveRecord::Base
belongs_to :course, inverse_of: :course_codes
validates_presence_of :course
end
Used like this.
像这样使用。
Course.create!({
course_codes_attributes: [{ code: "TDA123" }],
# ...
})
#2
-1
Looks good to me. Removing the validates_presence_of :course
might make things easier on you, too, as it will tend to get in the way an not add much.
在我看来很好。删除validates_presence_of:course也可能会让事情变得更容易,因为它往往会妨碍不添加更多内容。
When you create a course, do it like this:
创建课程时,请执行以下操作:
Course.create course_codes: [CourseCode.new(...), CourseCode.new(...)]
ActiveRecord will figure things out.
ActiveRecord会解决问题。
#3
-1
You could add an unless
to whichever model you would plan to create first. For instance:
您可以添加一个除非您计划首先创建的模型。例如:
class CourseCode < ActiveRecord::Base
belongs_to :course
validates_presence_of :course, :unless => lambda { Course.all.empty? }
end