По умолчанию соединение этих where
вместе создает И, что вы и хотите.
Так много быть:
class Company < ActiveRecord::Base
def self.where_1
where(...)
end
def self.where_2
where(...)
end
end
@companies = Company.where_1.where_2
====== ОБНОВЛЕНО ======
Есть два случая:
# case 1: the fields selecting are different
Company.where(:id => [1, 2, 3, 4]) & Company.where(:other_field => true)
# a-rel supports &, |, +, -, but please notice case 2
# case 2
Company.where(:id => [1, 2, 3]) & Company.where(:id => [1, 2, 4, 5])
# the result would be the same as
Company.where(:id => [1, 2, 4, 5])
# because it is &-ing the :id key, instead of the content inside :id key
Так что, если вы находитесь в случае 2, вам нужно будет сделать то, что прокомментировал @apneadiving.
Company.where(...).all & Company.where(...).all
Конечно, при этом отправляются два запроса и, скорее всего, запрашивается больше результатов, чем вам нужно.
person
PeterWong
schedule
22.06.2011