Rails Trick & Tips : Rails delegate delegate, rails tips & tricks
Rails delegate
So checkout this page : http://devblog.avdi.org/2011/07/05/demeter-its-not-just-a-good-idea-its-the-law/
Here, adding some delegate to the user class :
class Project < ActiveRecord::Base
belongs_to :user
belongs_to :owner, :class_name => 'User', :foreign_key => 'owner_id'
delegate :username, :to => :owner, :prefix => true, :allow_nil => true
delegate :username, :to => :user, :prefix => "author", :allow_nil => true
end
And now in the show.html.haml , we can use (with possibly null value) :
%p= @project.owner_username
%p= @project.author_usermane
Tweet
comments powered by Disqus