开发者

Trouble on using named 'scope' with in a call to an "external" method

开发者 https://www.devze.com 2023-04-04 11:14 出处:网络
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

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:

  1. Is it possible to call the method find_owned_by in the scope :not_rel named scope?

  2. How can I solve the ActiveRecord::StatementInvalid error?


Could the problem be related to the map(&:id) method called in the model?

0

精彩评论

暂无评论...
验证码 换一张
取 消