当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby - 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.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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: