Let's say you have two models: articles
and comments
.
class Article < ActiveRecord::Base
has_many :comments
end
You know you can fetch associated comments to an article like this:
article = Article.first
article.comments # => SELECT * FROM "comments" WHERE ("comments".article_id = 123)
Is there a way to explicitly access the article_id
(123) within a named_scope?
I need this for a complex named_scope that joins another table. Basically the named_scope will depend on to be called from the associated parent object to make sense (article.comments.my_named_scope
and not Comments.my_named_scope
).
I don't want to pass the id as a parameter for the named_scope. So, instead of passing the article_id
to the named scope with ... lambda { |article| ...}
and access the id with "... #{article.id} ..."
, I want to somehow access this article_id
that the others
method uses, which I get from the has_many
association.