モデルがあるとします:
class Question < ActiveRecord::Base
attr_accessible :title # it has title attribute
has_many :pictures
end
scope
私はそれと呼ばれるクエリを定義したいcompleted
:
次のすべての質問を返します。
- タイトルが空でない OR
- 少なくとも 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