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

发布于 2024-10-03 17:52:09 字数 616 浏览 1 评论 0原文

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

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

我用这个把自己扭成了椒盐卷饼,看起来很容易,但我已经看它太久了。我很感激任何帮助!

I have written quite a few AREL statements, but I'm tying myself in knots over this one. Here is my situation:

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

I want to retrieve all of the Products that have a child product with an identifier that matches some value.

I have twisted myself into a pretzel with this, seems easy, but I have been looking at it for too long now. I appreciate any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷血 2024-10-10 17:52:09

知道了!

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

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

Got it!

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

That works great. See the hashed joins? That's what was throwing me off.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文