如何在named_scope中返回布尔结果?

ker*_*lin 0 ruby named-scope boolean ruby-on-rails

named_scope :incomplete?, lambda { |user_id, todo_id| 
  { :select => 1, :conditions =>
    [ "#{user_id} not in (select user_todos.user_id from user_todos) and
       #{todo_id} not in (select user_todos.todo_id from user_todos)" ]
  } 
}
Run Code Online (Sandbox Code Playgroud)

我得到一个零结果.我希望它返回真实.我要做什么!?

还有,有更好的方法来写这个吗?

Sim*_*tti 5

您的代码存在一个巨大的问题:命名范围不是为了返回布尔值或单个值,而是用于返回要链接的过滤器.

请改用类方法.另外,使用插值,不要将值直接写入SQL代码.

class YourModel
  def self.incomplete?(user_id, todo_id)
    exists?(["? not in (select user_todos.user_id from user_todos) and ? not in (select user_todos.todo_id from user_todos)", user_id, todo_id])
  end
end
Run Code Online (Sandbox Code Playgroud)