I’m trying to find all values that are not nil.
I tried:
Task.find(:all, :conditions => [“user_id != ?, nil”])
but that doesn’t seem to be working. Any ideas?
I’m trying to find all values that are not nil.
I tried:
Task.find(:all, :conditions => [“user_id != ?, nil”])
but that doesn’t seem to be working. Any ideas?
On 9 Nov 2007, at 08:24, Bob S. wrote:
I’m trying to find all values that are not nil.
I tried:
Task.find(:all, :conditions => [“user_id != ?, nil”])
but that doesn’t seem to be working. Any ideas?
With mysql at least, NULL is a magical value, any comparison with NULL
always returns false. You need to use foo IS NULL or foo IS NOT NULL.
Fred
Xavier N. wrote:
On Nov 9, 2007, at 9:24 AM, Bob S. wrote:
I’m trying to find all values that are not nil.
I tried:
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.
I tried “Task.find_all_by_user_id(!nil)” – but of course, I’m a noob,
and that’s not working either.
On Nov 9, 2007, at 9:24 AM, Bob S. wrote:
I’m trying to find all values that are not nil.
I tried:
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
On Nov 9, 2007, at 11:03 AM, Bob S. wrote:
– 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 N. wrote:
On Nov 9, 2007, at 11:03 AM, Bob S. wrote:
– 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
No problem, Xavier. Thanks for your help though!
I really appreciate it. I guess I’ll do some sort of workaround, and see
how that goes. Thanks again!
You can just do:
Task.find(:all, :conditions => “user_id is not null”)
Cheers,
Gary.
Xavier N. <fxn@…> writes:
def self.unassigned_tasks
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 G.
Gary D. wrote:
You can just do:
Task.find(:all, :conditions => “user_id is not null”)
Cheers,
Gary.
Wowzers. I couldn’t even have imagined that was possible. Ruby’s crazy.
Thanks, Gary! You’re a lifesaver, man. Thank you!
On Nov 9, 2007, at 11:48 AM, Raimond G. wrote:
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]
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
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
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.
On Nov 9, 1:53 pm, tonypm [email protected] wrote:
assuming my app wont be running after 2055 - lol
Tonypm
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]
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.
Xavier,
Yes, not the most efficient way ;), that’s probably why my app runs so
slow
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs