开发者

named scope - passing params[:id]

开发者 https://www.devze.com 2023-03-06 13:33 出处:网络
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.

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消