Theo mặc định, hãy kết nối những người này where
với nhau tạo VÀ đó là những gì bạn muốn.
Vì vậy, nhiều là:
class Company < ActiveRecord::Base
def self.where_1
where(...)
end
def self.where_2
where(...)
end
end
@companies = Company.where_1.where_2
====== CẬP NHẬT ======
Có hai trường hợp:
# 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
Vì vậy, nếu bạn đang ở trong trường hợp 2, bạn sẽ cần phải làm như những gì @apneadiving bình luận.
Company.where(...).all & Company.where(...).all
Tất nhiên, việc này sẽ gửi hai truy vấn và nhiều khả năng truy vấn nhiều kết quả hơn bạn cần.
Có vẻ bạn không muốn sql đây. Vì vậy, bạn có thể sử dụng phương thức giao cắt mảng: http://www.ruby-doc.org/core/classes/Array.html#M000274 – apneadiving