你能帮我写下以下范围吗:
Cv
belongs_toStudent
并且我想写一个范围,它给我所有的 cvs,其中学生至少有一个教育(student.edcuations.any?
)并且学生是有效的(所有属性都被填充)
我想为Cv
.
楷模
#cv.rb
belongs_to :student
#student.rb
has_many :cvs
has_many :educations
你能帮我写下以下范围吗:
Cv
belongs_toStudent
并且我想写一个范围,它给我所有的 cvs,其中学生至少有一个教育(student.edcuations.any?
)并且学生是有效的(所有属性都被填充)
我想为Cv
.
楷模
#cv.rb
belongs_to :student
#student.rb
has_many :cvs
has_many :educations
我想我误解了你的模型的布局。
我觉得它应该看起来更像这样,因为每个简历都会列出一个教育(否则,数据库中的相互连接如何)
#cv.rb
belongs_to :student
has_many :educations
#student.rb
has_many :cvs
#education.rb
belongs_to :cvs
不过,我可能只是使用类方法。
#student.rb
def cvs_with_education
self.cvs.reject {|cv| cv.educations.empty?}
end
查看ActieRecord 查询接口指南的第 11.2.3 节,了解如何加入嵌套关联。
就像是
scope :with_education, joins(:student => :educations).where("some conditions")
您可能想要定义一个:valid_student
范围,然后在其中利用它。