宝石可以这样帮助:
class Book
belongs_to :author
end
book = Book.first
book.author_name #=> same as book.author.name
book.provide( :author_name, :title ) #=> will automatically map like this: { :title => book.title, :author_name => book.author.name }
有没有宝石帮助?谢谢!
答案 0 :(得分:1)
查看rails Delegate模块:
class Invoice < Struct.new(:client)
delegate :name, :address, :to => :client, :prefix => :customer
end
invoice = Invoice.new(john_doe)
invoice.customer_name # => "John Doe"
invoice.customer_address # => "Vimmersvej 13"