I feel like I''m missing something obvious here.. I have a Post object and it has a draft attribute. It may be nil, true or false. I want nil to be treated as false Post.where(:draft => false) does not pick up the posts where draft is unset(nil) This leaves me to query with Post.where(''draft is NOT true'') Is there a cleaner way to do this? I prefer using hash conditions. I could set the draft boolean before_save but it seems wasteful. Thanks in advance Tony -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 3, 2011, at 2:08 PM, Tony Primerano wrote:> I feel like I''m missing something obvious here.. > > I have a Post object and it has a draft attribute. It may be nil, > true or false. I want nil to be treated as false > > Post.where(:draft => false) does not pick up the posts where draft is > unset(nil) > > This leaves me to query with > > Post.where(''draft is NOT true'') > > Is there a cleaner way to do this? I prefer using hash conditions. > I could set the draft boolean before_save but it seems wasteful.Alter your database so that the default value for ''draft'' is false. Then update your database to set any nil values to false. -philip -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 3, 2011, at 3:08 PM, Tony Primerano wrote:> Post.where(''draft is NOT true'')That shouldn''t work either. NULL is neither true nor false. "draft = false or draft is null" is the SQL for what you''re asking for. But if NULL really has no meaning that is distinct from false, then it''s an incorrect design to allow it in the first place. -- Scott Ribe scott_ribe-ZCQMRMivIIdUL8GK/JU1Wg@public.gmane.org http://www.elevated-dev.com/ (303) 722-0567 voice -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Yeah. I guess ruby is making me lazy. ;-) I''ll set all my NULLs to false and validate presence. On Jan 3, 5:42 pm, Scott Ribe <scott_r...-oLpdKesOropn0q4rP9l2pw@public.gmane.org> wrote:> On Jan 3, 2011, at 3:08 PM, Tony Primerano wrote: > > > Post.where(''draft is NOT true'') > > That shouldn''t work either. NULL is neither true nor false. "draft = false or draft is null" is the SQL for what you''re asking for. > > But if NULL really has no meaning that is distinct from false, then it''s an incorrect design to allow it in the first place. > > -- > Scott Ribe > scott_r...-ZCQMRMivIIdUL8GK/JU1WhHnuRYL88vP@public.gmane.org://www.elevated-dev.com/ > (303) 722-0567 voice-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tony Primerano wrote in post #972129:> Yeah. I guess ruby is making me lazy. ;-) > > I''ll set all my NULLs to false and validate presence.Remember to make the field NOT NULL in the DB. Rails validations are insufficient. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.