0
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)" ]
  } 
}

我得到一个零结果。我希望它返回 true。我该怎么办!?

另外,有没有更好的方法来写这个?

4

2 回答 2

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
于 2010-01-15T09:07:27.627 回答
0

谢谢。我发现了问题!我最终写了这个:

def不完整?(user_id, todo_id) return UserTodo.find_by_sql("select case when (#{user_id} not in (select user_id from user_todos)) and (#{todo_id} not in (select todo_id from user_todos) then true else false以 user_todos 的不完整结尾") 结束

但我更喜欢你的方法。

于 2010-01-16T20:36:23.380 回答