开发者

How to select * from a database where a column != a value in rails

开发者 https://www.devze.com 2023-03-14 06:57 出处:网络
Right now I\'m doing the following query to display all the tasks in a simple project management app I\'m working on.

Right now I'm doing the following query to display all the tasks in a simple project management app I'm working on.

Task.find(:all, :order => 'updated_at DESC')

This is fine and works great although I have a private column that is set to 1 if the task is private. How do I select all the tasks in the database where private doesn't == 开发者_如何转开发1?


This ARel should do the trick:

Task.where(:private => false).order("updated_at DESC")

Edit: That's assuming your column is either 1 or 0. If not, you can use the lt/gt comparisons, which will look something like this:

Task.where(Task.arel_table[:private].gt(1)).order("updated_at DESC")


muffinista's solution is the best if you're using Rails 3. if you're still on Rails 2, you'll need to use conditions:

Task.find(:all, :conditions => {:private => false}, :order => 'updated_at DESC')

or

Task.find(:all, :conditions => "private != 1", :order => 'updated_at DESC')
0

精彩评论

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