如何在自引用表上编写AREL语句

时间:2010-11-21 02:57:29

标签: ruby-on-rails-3 arel

我已经写了很多AREL声明,但是我在结束这个声明。这是我的情况:

class Product < AR::Base
  has_many :parents, :class_name => "ProductLink", :foreign_key => :to_product_id
  has_many :children, :class_name => "ProductLink", :foreign_key => :from_product_id

  # has an attribute called "identifier"

end

class ProductLink < AR::Base
  belongs_to :parent, :class_name => "Product", :foreign_key => :from_product_id
  belongs_to :child, :class_name => "Product", :foreign_key => :to_product_id
end

我想要检索所有具有标识符匹配某个值的子产品的产品。

我把自己扭成了一个椒盐脆饼干,看起来很简单,但我现在已经看了很久了。我感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

知道了!

brand.products.joins(:children => :child).where(:children => { :child => { :searchable_identifier.matches => "2136" } } )

这很有效。看到哈希连接?这就是让我失望的原因。