How to use a scope in combination with a Model method?

Posted by Bjorn on Stack Overflow See other posts from Stack Overflow or by Bjorn
Published on 2011-01-04T08:32:24Z Indexed on 2011/01/04 8:53 UTC
Read the original article Hit count: 441

Filed under:
|
|

Hi all,

I'm currently working on some scope filters, and I'd like to have it filter on a method (don't know how to name it otherwise). My code:

class Product < ActiveRecord::Base
  def price_with_discount
    price-discount.to_f
  end

  scope :by_price, lambda {|min,max|{ :conditions => { :price_with_discount => min.to_f..max.to_f }}}
end

This doesn't work: "No attribute named price_with_discount exists for table products". How can I trick my scope into using the method I defined instead of searching for a column named price_with_discount?

Bjorn

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby