Предположим, у меня есть модель:
class Question < ActiveRecord::Base
attr_accessible :title # it has title attribute
has_many :pictures
end
Я хочу определить запрос scope
с именем completed
, который:
Возвращает все вопросы, чьи:
- заголовок не пустой ИЛИ
- есть хотя бы 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