问题描述:

How do I convert the following Rails 2 code to Rails 3 scope, I'm trying to remove .merge(:conditions) and move entirely to Rails 3 activerecord scope.

class Customer < ActiveRecord::Base

def self.find_invoice_by_customer(customer_address, opts={})

invoice = Customer.find(opts.merge(:conditions => {:address => customer_address }))

end

end

Customer.find_invoice_by_customer(@address, :condition => ["customer_name = ?", @customer.name])

网友答案:

You could use scopes and the new finder methods to chain them:

class Customer < ActiveRecord::Base    
  scope :by_address, lambda {|address| {:conditions => {:address => address }}
end

Customer.by_address(@address).where("customer_name = ?", @customer.name)

Does this help ? I recommend you have a look at the documentation and the Railscast about Active Record.

相关阅读:
Top