Taylor Strait
2009-Jan-05 18:48 UTC
checking booleans while maintaining database compatibility
Users have many invitations and Invitations belongs_to a User. In the user model: has_many :open_invitations, :class_name => ''Invitation'', :conditions => "completed_at IS NULL AND is_closed = ''f''" This works for SQLite3. But as a literal string this will fail if the boolean values are different in another db system like MySQL, right? How can I use a boolean in a condition that will work across all db types? -- 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 -~----------~----~----~----~------~----~------~--~---
Danny Burkes
2009-Jan-05 19:13 UTC
Re: checking booleans while maintaining database compatibility
Taylor Strait wrote:> > has_many :open_invitations, > :class_name => ''Invitation'', > :conditions => "completed_at IS NULL AND is_closed = ''f''" > > This works for SQLite3. But as a literal string this will fail if the > boolean values are different in another db system like MySQL, right? > How can I use a boolean in a condition that will work across all db > types?I have done things like http://pastie.org/353140 Best Regards, Danny -- 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 -~----------~----~----~----~------~----~------~--~---
Craig Demyanovich
2009-Jan-05 19:26 UTC
Re: checking booleans while maintaining database compatibility
On Mon, Jan 5, 2009 at 2:13 PM, Danny Burkes < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Taylor Strait wrote: > > > > has_many :open_invitations, > > :class_name => ''Invitation'', > > :conditions => "completed_at IS NULL AND is_closed = ''f''" > > > > This works for SQLite3. But as a literal string this will fail if the > > boolean values are different in another db system like MySQL, right? > > How can I use a boolean in a condition that will work across all db > > types? > > I have done things like http://pastie.org/353140:conditions => ["completed_at IS NULL AND is_closed = ?", false] works across databases when finding records. Does it work in a has_many declaration? has_many :open_invitations, :class_name => ..., :conditions => ["completed_at IS NULL AND is_closed = ?", false] Regards, Craig --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Taylor Strait
2009-Jan-05 19:38 UTC
Re: checking booleans while maintaining database compatibili
Craig Demyanovich wrote:> On Mon, Jan 5, 2009 at 2:13 PM, Danny Burkes < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> > types? >> >> I have done things like http://pastie.org/353140 > > > :conditions => ["completed_at IS NULL AND is_closed = ?", false] works > across databases when finding records. Does it work in a has_many > declaration? > > has_many :open_invitations, :class_name => ..., :conditions => > ["completed_at IS NULL AND is_closed = ?", false] > > Regards, > CraigThanks for the replies! Craig''s solution worked - you CAN use a "safe array" in the conditions of has_many. The final associations look like this and have been confirmed working in the console: class User has_many :invitations has_many :open_invitations, :class_name => ''Invitation'', :conditions => ["completed_at IS NULL AND is_closed = ?", false] has_many :closed_invitations, :class_name => ''Invitation'', :conditions => [''completed_at IS NOT NULL OR is_closed = ?'', true] -- 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 -~----------~----~----~----~------~----~------~--~---
Danny Burkes
2009-Jan-05 19:39 UTC
Re: checking booleans while maintaining database compatibility
Craig Demyanovich wrote:> :conditions => ["completed_at IS NULL AND is_closed = ?", false] works > across databases when finding records. Does it work in a has_many > declaration? >Excellent, thanks- more code I can eliminate :-) - D -- 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 -~----------~----~----~----~------~----~------~--~---