views:

250

answers:

1
named_scope :correct, :include => :correction, :conditions => "checked_at IS NOT NULL AND corrections.id IS NULL"

On a side note I have googled loads and looked through books but i cant seem to find a list of all the various types of conditions you can use and how they differ when implenting them as strings, arrays or hashes.

Is there a list of the syntax anywhere?

+3  A: 

The string you posted is correct. Also, there's no way to express the same condition using arrays or hashes.

Array–syntax and Hash-syntax are useful when you need to interpolate values. For instance, the following condition

named_scope :is_one, :conditions => "field = '1'"

can be written as

named_scope :is_one, :conditions => ["field = ?", "1"]

or

named_scope :is_one, :conditions => { :field => "1" }

The Hash-syntax is a subset of the Array-syntax and supports only a limited set of operators. For instance, you can transform

named_scope :is_one, :conditions => ["field1 = ? AND field2 IN (?)", "1", ["foo", "bar"]]

into

named_scope :is_one, :conditions => { :field1 => "1", :field2 => ["foo", "bar"] }

but there's no Hash-equivalent for

# OR
named_scope :is_one, :conditions => ["field1 = ? OR field2 IN (?)", "1", ["foo", "bar"]]
# <>
named_scope :is_one, :conditions => ["field1 <> ?", "1"]
Simone Carletti
thank you very much simone for explaining that. Thats really helped me a lot!
adam