Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:使用.where方法重写find_all_by()_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails Rails:使用.where方法重写find_all_by()

Ruby on rails Rails:使用.where方法重写find_all_by(),ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我使用的gem不适用于find_all_by()方法中的数组,但适用于.where();然而,我不知道如何用产生相同结果的方式来编写它 例如,我如何重写: Post.find_all_by_poster(@user.id , :conditions => ['title IS NOT NULL OR name !=?', 'Bob' ]) 我的尝试: Post.where("poster = ? AND title !=? OR name !=?", @user.id, 'NULL',

我使用的gem不适用于find_all_by()方法中的数组,但适用于.where();然而,我不知道如何用产生相同结果的方式来编写它

例如,我如何重写:

Post.find_all_by_poster(@user.id , :conditions => ['title IS NOT NULL OR name !=?', 'Bob' ])
我的尝试:

Post.where("poster = ? AND title !=? OR name !=?", @user.id, 'NULL', 'Bob')

有什么想法吗?我很难满足同样的条件。谢谢你的帮助

您仍然需要使用
IS NOT NULL
而不是作为单独的参数传入
NULL

Post.where("poster = ? and (title is not null or name != ?)", @user.id, 'Bob') 

您仍然需要使用
IS NOT NULL
,而不是将
NULL
作为单独的参数传入:

Post.where("poster = ? and (title is not null or name != ?)", @user.id, 'Bob') 

研究如何避免在代码中编写SQL片段。感谢您的建议,它似乎真的可以帮助提高我的工作效率!我将开始使用itLook进行练习,以避免在代码中编写SQL片段。感谢您的建议,它似乎真的可以帮助提高我的工作效率!我将开始练习它