Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使用两个ActiveRelation结果返回另一个关系_Ruby On Rails_Activerecord_Arel - Fatal编程技术网

Ruby on rails 如何使用两个ActiveRelation结果返回另一个关系

Ruby on rails 如何使用两个ActiveRelation结果返回另一个关系,ruby-on-rails,activerecord,arel,Ruby On Rails,Activerecord,Arel,假设我有以下代码 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 类产品country.id) 结束 国家/地区(国

假设我有以下代码

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
类产品country.id)
结束
国家/地区(国家/地区)的自我定义
其中(:destination\u country\u id=>:country.id)
结束
结束
如果我想把产品生产并分发到德国,我可以做以下几点

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...>...]
Product.for_country.from_country=>ActiveRecord::Relation
products=Product.for_country.from#=>数组[…]
在上面的例子中,如果我愿意,我可以在分配给产品之前链接更多的关系方法

如果我想访问所有涉及德国的产品,我可以执行以下操作

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...>...]
Product.for|u country | Product.from#=>数组[…]
products=Product.for|u country | Product.from#=>数组[…]
在这里,我无法在将其分配给产品之前链接更多的关系方法,因为OR的结果是
数组
而不是
ActiveRecord
::关系

我的问题是,如何使用from-country或for-country获得
ActiveRecord::Relation

理想情况下,类似以下
产品,用于国家/地区或(来自国家/地区的产品)


我知道这并不是你问题的确切答案,但我想说另一种方法,比如使用这种“或”逻辑的“代码>涉及国家”的方法会使你的其他代码比链接来自国家和国家的代码更易读。那些被链接的方法并不能很好地解释它们自己。

这些代码只是我实际尝试做的一个示例,不是真正的代码。你所举的例子当然是对的。