假设我有一个模型:
class Question < ActiveRecord::Base
attr_accessible :title # it has title attribute
has_many :pictures
end我想要定义一个名为scope的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如果我能说:
class Question < ActiveRecord::Base
attr_accessible :title # it has title attribute
has_many :pictures
scope :completed, where{title != "" || pictures.count > 0}
end发布于 2011-07-19 13:22:00
你当然可以用吱吱声来做这件事!只需这样写你的范围:
scope :completed, joins{pictures.outer}.where{(title != "") | (pictures.id != nil)}.group{id}希望我帮了你。
https://stackoverflow.com/questions/6710247
复制相似问题