byCoder
byCoder

Reputation: 9184

Activerecord check if value is not true

How to correctly check value in db: in table i have field add_after_sign_in which is boolean, by default it is false... but how to check if it is not empty and not true, then select such rows: i try:

litem = LineItem.find(:all, :conditions => {:cart_id => cart_id && !:add_after_sign_in })

but in litem there are no such records... why? Simple how to check table row value on non-true?

Upvotes: 0

Views: 1036

Answers (3)

ichigolas
ichigolas

Reputation: 7725

You can also use where for a more readable syntax:

litem = LineItem.where(cart_id: cart_id, add_after_sign_in: [nil, false])

or, with scopes:

class LineItem
  scope :not_added_after_sign_in, where(add_after_sign_in: [nil, false])
  #...
end

litem = LineItem.where(cart_id: cart_id).not_added_after_sign_in

Edit: tweaked after tokland's answer.

Upvotes: 1

tokland
tokland

Reputation: 67860

You cannot use symbols like this (but note that you can do similar things with squeel and Rails 3).

I'd write (note that it's line_items, it's a collection):

line_items = LineItem.where(:cart_id => cart_id, :add_after_sign_in => false)

However, with a sound model structure and named scopes you should be able to write more declarative code:

line_items = Cart.find(cart_id).line_items.without_add_after_sign_in

Upvotes: 1

MurifoX
MurifoX

Reputation: 15089

conditions is a Hash. You are only messing up with the syntax. Try this one:

litem = LineItem.find(:all, 
  :conditions => {:cart_id => cart_id, :add_after_sign_in => [nil, false]})

Upvotes: 1

Related Questions