delegate的使用

我们有时候会遇到这种情况,如下

info.user.name
info.user.age
我们可以让代码变得更漂亮些或者你想重构的更好些,那么如下:

class Info< ActiveRecord::Base
   belongs_to :user
   delegate :name, :address :to => :user, :prefix => true
end

<%= @info.user_name %>
<%= @info.user_address %>


prifix 参数:   flase(默认),则不加前缀,也就是 info.name
                true,加前缀,也就是 info.user_name

你可能感兴趣的:(ActiveRecord)