【发布时间】:2010-02-01 23:07:27
【问题描述】:
假设您有两个模型:articles 和 comments。
class Article < ActiveRecord::Base
has_many :comments
end
您知道您可以将相关的 cmets 提取到这样的文章中:
article = Article.first
article.comments # => SELECT * FROM "comments" WHERE ("comments".article_id = 123)
有没有办法在 named_scope 内显式访问article_id (123)?
对于连接另一个表的复杂 named_scope,我需要它。基本上,named_scope 将取决于从关联的父对象调用是否有意义(article.comments.my_named_scope 和 not Comments.my_named_scope)。
我不想将 id 作为named_scope 的参数传递。因此,我不想使用... lambda { |article| ...} 将article_id 传递给命名范围并使用"... #{article.id} ..." 访问id,而是想以某种方式访问others method 使用的article_id,我从has_many 获得协会。
【问题讨论】:
标签: ruby-on-rails associations named-scope