Forum: Ruby on Rails checking booleans while maintaining database compatibility

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (taylorstrait)
on 2009-01-05 19:48
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?
16b26104470ae1b64958f3caa836c193?d=identicon&s=25 Danny Burkes (dburkes)
on 2009-01-05 20:13
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
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2009-01-05 20:26
(Received via mailing list)
On Mon, Jan 5, 2009 at 2:13 PM, Danny Burkes <
rails-mailing-list@andreas-s.net> 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,
Craig
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (taylorstrait)
on 2009-01-05 20:38
Craig Demyanovich wrote:
> On Mon, Jan 5, 2009 at 2:13 PM, Danny Burkes <
> rails-mailing-list@andreas-s.net> 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,
> Craig

Thanks 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]
16b26104470ae1b64958f3caa836c193?d=identicon&s=25 Danny Burkes (dburkes)
on 2009-01-05 20:39
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
This topic is locked and can not be replied to.