开发者

How do I convert Rails 2 hash merge to Rails 3?

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

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.

0

精彩评论

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