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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 9 Nov 2007, at 08:24, Bob Sanders 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 9, 2007, at 9:24 AM, Bob Sanders 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Xavier Noria wrote:> On Nov 9, 2007, at 9:24 AM, Bob Sanders 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 > > -- fxnHi 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 9, 2007, at 11:03 AM, Bob Sanders wrote:>>> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Xavier Noria wrote:> On Nov 9, 2007, at 11:03 AM, Bob Sanders 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! > > -- fxnNo 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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Xavier Noria <fxn@...> writes:> > > On Nov 9, 2007, at 11:03 AM, Bob Sanders wrote: > > >>> 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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You can just do: Task.find(:all, :conditions => "user_id is not null") Cheers, Gary. Bob Sanders wrote:> Xavier Noria wrote: >> On Nov 9, 2007, at 11:03 AM, Bob Sanders 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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Gary Doades 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! :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 9, 2007, at 11:48 AM, Raimond Garcia 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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:> Gary Doades 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! :)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 9, 1:53 pm, tonypm <tonypmar...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> 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> 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 :D 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---