Trouble on using named 'scope' with in a call to an "external" method
I am using Ruby on Rails 3.0.10 and I am trying to implement a scope
method in a my model (say model A
) by including in that a call to a method present in another model (say model B
). That is, in A
I have:
scope :not_rel, lambda { |article, user| where("articles.id NOT IN (?)", article.find_owned_by(user).map(&:id)) }
The find_owned_by
method is stat开发者_运维技巧ed in B
and serves to search articles owned by a user. However, by using the above code, I get a ActiveRecord::StatementInvalid (Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT ...
error.
So, my question are:
Is it possible to call the method
find_owned_by
in thescope :not_rel
named scope?How can I solve the
ActiveRecord::StatementInvalid
error?
Could the problem be related to the map(&:id)
method called in the model?
精彩评论