title: Polymorphic association in rails4
body: <p>I'm following this tutorial <a href="http://blog.endpoint.com/2012/01/ruby-on-rails-rights-attributes.html" rel="nofollow">http://blog.endpoint.com/2012/01/ruby-on-rails-rights-attributes.html</a> to setup an authorization mechanism for my rails4 application.  </p>
  
  <p>I have created the data model but do not manage (from the console) to get the rights (via the all_rights method) of a user.  </p>
  
  <p>In the user model, how can the User object call "self.rights" as the rights are available through RightAssignment and not directly from User ? </p>
  
  <p>My models:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :right_assignments, as: :subject
    has_and_belongs_to_many :groups
    has_and_belongs_to_many :roles
  
    def all_rights
      rights = [self.rights +
            self.groups.collect { |g| g.allowed_rights } +
            self.roles.collect { |r| r.rights }]
       rights = rights.flatten.uniq.collect { |r| r.action }
       rights
    end
  end
  
  class Group &lt; ActiveRecord::Base
    has_many :right_assignments, as: :subject
    has_and_belongs_to_many :users
  
    def allowed_rights
      self.assignable_rights ? self.rights : []
    end
  end
  
  class Role &lt; ActiveRecord::Base
    has_many :right_assignments, as: :subject
    has_and_belongs_to_many :users
  end
  
  class RightAssignment &lt; ActiveRecord::Base
    belongs_to :right
    belongs_to :subject, polymorphic: true
  end
  
  class Right &lt; ActiveRecord::Base
    has_many :right_assignments
  end
  </code></pre>
  
  <p>Any idea ?</p>