Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.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 Rails通过关系基于中存在的集合进行查找_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails Rails通过关系基于中存在的集合进行查找

Ruby on rails Rails通过关系基于中存在的集合进行查找,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我试图根据包含集合的实例的关系选择实例。下面是一个简化的示例: class Product::Variation < ActiveRecord::Base attr_accessible :name, :product_id, :quantity belongs_to :product has_many :bids, :foreign_key => :product_variation_id has_many :product_variation_property_

我试图根据包含集合的实例的关系选择实例。下面是一个简化的示例:

class Product::Variation < ActiveRecord::Base
  attr_accessible :name, :product_id, :quantity

  belongs_to :product
  has_many :bids, :foreign_key => :product_variation_id
  has_many :product_variation_property_values, :class_name => 'Product::Variation::PropertyValue'
  has_many :property_values, :through => :product_variation_property_values, :class_name => 'Property::Value'
end

class Product::Variation::PropertyValue < ActiveRecord::Base
  attr_accessible :property_value_id, :variation_id, :property_id

  belongs_to :variation
  belongs_to :property_value, :class_name => 'Property::Value'
end

class Property < ActiveRecord::Base
  attr_accessible :name

  has_many :values, :class_name => 'Property::Value'
end

class Property::Value < ActiveRecord::Base
  attr_accessible :content

  belongs_to :property
  belongs_to :partner
end
有没有一种方法可以使用ActiveRecord实现这一点

谢谢,如果你需要更多信息,请告诉我

更多信息

我尝试了以下方法:

Product::Variation.joins(:property_values).where('property_values.id' => [Property::Value.find(1).id, Property::Value.find(2).id]).first
…这是以下SQL

SELECT "product_variations".* FROM "product_variations" INNER JOIN "product_variation_property_values" ON "product_variation_property_values"."variation_id" = "product_variations"."id" INNER JOIN "property_values" ON "property_values"."id" = "product_variation_property_values"."property_value_id" WHERE "property_values"."id" IN (1, 2)
…而这返回

#<Product::Variation id: 25, product_id: 1, quantity: 39, created_at: "2013-11-18 00:18:45", updated_at: "2013-11-18 00:18:45">
…我得到

[#<Property::Value id: 1, property_id: 1, content: "XS", created_at: "2013-11-18 00:18:45", updated_at: "2013-11-18 00:18:45", color: nil, color_texture: nil, secondary_color: nil>, #<Property::Value id: 6, property_id: 2, content: "Dark Wood", created_at: "2013-11-18 00:18:45", updated_at: "2013-11-18 00:18:45", color: "#855E42", color_texture: "striped", secondary_color: "#FFB90F">]

但我正在寻找同时包含属性::值1和2的Product::变体。这将返回包含1或2的数据。

这应该可以通过连接查询实现。很难写出来,但应该是这样的:

Variation.joins(propert_values: :values).where('values.id' => [Property::Value.find(1).id, Property::Value.find(2).id]).first

我尝试了你建议的一些编辑,但是在上面的更多信息下看到了我的结果。
[#<Property::Value id: 1, property_id: 1, content: "XS", created_at: "2013-11-18 00:18:45", updated_at: "2013-11-18 00:18:45", color: nil, color_texture: nil, secondary_color: nil>, #<Property::Value id: 6, property_id: 2, content: "Dark Wood", created_at: "2013-11-18 00:18:45", updated_at: "2013-11-18 00:18:45", color: "#855E42", color_texture: "striped", secondary_color: "#FFB90F">]
Variation.joins(propert_values: :values).where('values.id' => [Property::Value.find(1).id, Property::Value.find(2).id]).first