次のコードがあるとします
class Product < ActiveRecord::Base
def self.from_country(country)
where(:origin_country_id => country.id)
end
def self.for_country(country)
where(:destination_country_id => :country.id)
end
end
製品を製造してドイツに流通させたい場合は、次のことができます。
Product.for_country.from_country #=> ActiveRecord::Relation
products = Product.for_country.from_country #=> Array[<Product...>...]
上記の場合、必要に応じて、製品に割り当てる前に、より多くのリレーショナル メソッドを連鎖させることができます。
ドイツに関連するすべての製品にアクセスしたい場合は、次のようにします。
Product.for_country | Product.from_country #=> Array[<Product...>...]
products = Product.for_country | Product.from_country #=> Array[<Product...>...]
ここでは、OR の結果が::RelationではArray
ないため、製品に割り当てる前にリレーショナル メソッドを連鎖させることはできません。ActiveRecord
私の質問は、for_country と from_country を OR しActiveRecord::Relation
て、結果として a を取得するにはどうすればよいですか?
理想的には、次のようなものProduct.for_country(country).or(Product.from_country(country))