【问题标题】:How to write an AREL statement on a self-referencing table如何在自引用表上编写 AREL 语句
【发布时间】:2011-05-13 06:43:27
【问题描述】:

我已经写了很多 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

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

我已经用这个把自己变成了一个椒盐脆饼,看起来很容易,但我已经看了太久了。感谢您的帮助!

【问题讨论】:

    标签: ruby-on-rails-3 arel


    【解决方案1】:

    知道了!

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

    效果很好。看到散列连接了吗?这就是让我失望的原因。

    【讨论】:

    • 你不能在符号上调用matches...?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    相关资源
    最近更新 更多