在调用“外部”方法时使用命名“范围”的麻烦

时间:2011-09-12 11:08:40

标签: ruby-on-rails ruby ruby-on-rails-3 scope

我正在使用Ruby on Rails 3.0.10,我试图在我的模型中实现scope方法(比如模型A),方法是调用另一个方法中存在的方法模型(比如模型B)。也就是说,在A我有:

scope :not_rel, lambda { |article, user| where("articles.id NOT IN (?)", article.find_owned_by(user).map(&:id)) }

find_owned_by方法在B中说明,用于搜索用户拥有的文章。但是,通过使用上面的代码,我收到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 ...错误。

所以,我的问题是:

  1. 是否可以在find_owned_by命名范围内调用方法scope :not_rel

  2. 如何解决ActiveRecord::StatementInvalid错误?


  3. 问题可能与模型中调用的map(&:id)方法有关吗?

0 个答案:

没有答案