2011-05-15 93 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)