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?

2
  • 1
    Why do you want to use the array format if there's no variable aspect of the condition? Commented Apr 27, 2010 at 7:54
  • it was just for examples sake really. Commented Apr 27, 2010 at 13:26

1 Answer 1

3

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"]
Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much simone for explaining that. Thats really helped me a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.