有没有可以帮助Activerecord Association属性映射的gem?

时间:2009-09-21 02:57:23

标签: ruby-on-rails activerecord

宝石可以这样帮助:

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 }

有没有宝石帮助?谢谢!

1 个答案:

答案 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"