2011-05-15 71 views
0

在我的Rails应用程序中,我使用了命名范围。命名范围 - 传递参数[:id]

我想知道是否有可能传递参数,例如params [:id]或@ batch.batch_id到指定的范围。

image.rb:

named_scope :batch_images, lambda { 
    { :conditions => ["IMG_BATCH = ?",@batch.batch_id ] 
    } 
} 

目前上面的代码是给我为零的错误消息 '未定义的方法`BATCH_ID':NilClass。

的帮助

回答

8
named_scope :batch_images, lambda {|batch| where("IMG_BATCH = ?", batch.batch_id) } 

UPD非常感谢Rails的3+

scope :batch_images, ->(batch) { where("IMG_BATCH = ?", batch.batch_id) } 

然后使用Image.batch_images(your_batch)