Rails中的代理

阅读更多
Rails中的代理:
  Active Record中一个代理应用实例
  class Greeter < ActiveRecord::Base   
    def hello()   
      "hello"  
    end
    def goodbye()   
      "goodbye"  
    end  
  end  

  class Foo < ActiveRecord::Base   
    belongs_to :greeter  
    delegate :hello, :to => :greeter  
  end  

  Foo.new.hello   # => "hello"   
  Foo.new.goodbye # => NoMethodError: undefined method `goodbye' for #   
 
  class Foo < ActiveRecord::Base   
    belongs_to :greeter 
    delegate :hello, :goodbye, :to => :greeter  
  end  
  Foo.new.goodbye # => "goodbye"   

你可能感兴趣的:(Rails,ActiveRecord)