Jon
Jon

Reputation: 3985

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

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?

Upvotes: 2

Views: 84

Answers (2)

Taryn East
Taryn East

Reputation: 27747

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')

Upvotes: 1

muffinista
muffinista

Reputation: 6736

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")

Upvotes: 5

Related Questions