In my Rails app, am using named scope.
I want to know whether it's possible to pass a parameter such as params[:id] or @batch.batch_id to the named scope.
image.rb:
named_scope :batch_images, lambda {
{ :conditions => ["IMG_BATCH = ?",@batch.batch_id ]
}
}
Currently开发者_StackOverflow社区 the code above is giving me an error message 'undefined method `batch_id' for nil:NilClass.
Many thanks for your help
named_scope :batch_images, lambda {|batch| where("IMG_BATCH = ?", batch.batch_id) }
UPD For Rails 3+:
scope :batch_images, ->(batch) { where("IMG_BATCH = ?", batch.batch_id) }
Then use Image.batch_images(your_batch)
精彩评论