【发布时间】:2011-07-15 16:32:33
【问题描述】:
假设我有一个模型:
class Question < ActiveRecord::Base
attr_accessible :title # it has title attribute
has_many :pictures
end
我想定义一个名为 completed 的 scope 查询:
返回所有符合以下条件的问题:
- 标题不为空 或
- 至少有 1 张图片
我该怎么做?
到目前为止,我有:
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
【问题讨论】:
标签: ruby-on-rails-3.1 arel squeel