named_scope find options

Let’s say you have


named_scope :red, :conditions => {:color => 'red'}

Do not do this:


Shirt.red(:limit => 1) #=> ALL red shirts

You will get all red shirts. I don’t care how many you limit to, you will get them all. Instead, you need to chain using find(:all) or it’s alias all:


Shirt.red.all(:limit => 1) #=> ONE red shirt

This has been your Rails public service announcement for 17 September 2008.

  1. talby reblogged this from dstrelau and added:
    Wouldn’t it be easier...do this:? Shirt.red.first/last
  2. dstrelau posted this