How do you find all values that are not nil?

You've already told the SQL is wrong (even the quotes are in the wrong place). Nevertheless dynamic finders make this easier:

   def self.unassigned_tasks      Task.find_all_by_user_id(nil)    end

-- fxn

Task.find(:all, :conditions => ["user_id != ?, nil"])

You've already told the SQL is wrong (even the quotes are in the wrong place). Nevertheless dynamic finders make this easier:

  def self.unassigned_tasks     Task.find_all_by_user_id(nil)   end

-- fxn

Hi Xavier,

I saw that I misplaced the quotes. My bad about it.

I'm actually looking for all values that are NOT nil.

Heh :-), my fault I didn't see the negation.

I tried "Task.find_all_by_user_id(!nil)" -- but of course, I'm a noob, and that's not working either.

Yep, dynamic finders and conditions as hashes do not support negations. You know, the method just sees a regular value, the result of the expression !nil which is computed before the method is called. They do not generate "is different than" SQL operators either.

For nils they are handy because you get "==" or "IS NULL" as needed, whereas in the string/array notation you write the operator by hand, but for IS NOT NULL you can't use them.

Sorry!

-- fxn

Xavier Noria <fxn@...> writes:

>>> Task.find(:all, :conditions => ["user_id != ?, nil"]) >> >> You've already told the SQL is wrong (even the quotes are in the
>> wrong >> place). Nevertheless dynamic finders make this easier: >> >> def self.unassigned_tasks >> Task.find_all_by_user_id(nil) >> end >> >> -- fxn > > Hi Xavier, > > I saw that I misplaced the quotes. My bad about it. > > I'm actually looking for all values that are NOT nil.

Heh , my fault I didn't see the negation.

> I tried "Task.find_all_by_user_id(!nil)" -- but of course, I'm a noob, > and that's not working either.

Yep, dynamic finders and conditions as hashes do not support
negations. You know, the method just sees a regular value, the result
of the expression !nil which is computed before the method is called.
They do not generate "is different than" SQL operators either.

For nils they are handy because you get "==" or "IS NULL" as needed,
whereas in the string/array notation you write the operator by hand,
but for IS NOT NULL you can't use them.

Sorry!

Hi Xavier,

You can either use reject or compact, check out this examples:

test = [1,2,nil,3] test.reject {|x| x.nil?} => [1, 2, 3]

or

test.compact => [1, 2, 3]

Now, keep in mind that this methods won't modify the test array, it will only return the non-nil values, if you want to modify the test array itself, use the bang methods: reject! or compact!

Regards,

Raimond Garcia

You can just do:

Task.find(:all, :conditions => "user_id is not null")

Cheers, Gary.

Bob Sanders wrote:

Yep, but how does that relate to this thread? Keep in mind that unless your table is really small it is not a good idea to find(:all) + reject, you normally express that in the first place with SQL. You normally want to fetch the exact needed collection of data from the database.

-- fxn

You're welcome.

It's more SQL than ruby though. Don't forget that you can put pretty much any SQL in the :conditions clause, additional '?' parameters are optional.....

Cheers, Gary.

Bob Sanders wrote:

It's more SQL than ruby though. Don't forget that you can put pretty much any SQL in the :conditions clause, additional '?' parameters are optional.....

Same is true for :order I needed to sort objects with latest dates at the top, but with nil dates to be above these.

:order=>"IFNULL(completed_date, '20550101') desc"

assuming my app wont be running after 2055 - lol

Tonypm

Xavier,

Yes, not the most efficient way ;), that's probably why my app runs so slow :smiley: I saw this statement in my legacy code @users.compact! and that inspired my response, but I guess I should look at why there were nil users to start with, probably using Gary statement Task.find(:all, :conditions => "user_id is not null") will fix the problem.

Thanks for pointing out the inefficiency, I'll keep it in mind,

Rai