假设我有一个模型:
class Question < ActiveRecord::Base
attr_accessible :title # it has title attribute
has_many :pictures
end
我想定义名为scope
的{{1}}查询:
返回所有问题:
- 标题不为空 OR
- 至少有1张图片
我该怎么做?
到目前为止,我有:
completed
如果我能说:
,那就太好了class Question < ActiveRecord::Base
attr_accessible :title # it has title attribute
has_many :pictures
scope :completed, where{title != ""} # returns all questions with non-empty title
end
答案 0 :(得分:6)
当然你可以用Squeel做到这一点!只需用这种方式编写范围:
scope :completed, joins{pictures.outer}.where{(title != "") | (pictures.id != nil)}.group{id}
希望我帮助过。