How do I convert Rails 2 hash merge to Rails 3?
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.fin开发者_运维问答d_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.
精彩评论