title: Is it possible to create a conditional association in model?
body: <p>I have setup a role based access controll system with the following models:</p>
  
  <ul>
  <li>Role (as STI), 
  <ul>
  <li>UserRole (global roles)</li>
  <li>ProjectRole (project specific roles)</li>
  </ul></li>
  <li>Assignment (Polymorphic with different resources)</li>
  <li>User</li>
  <li>Project (as one resource type for assignments)</li>
  </ul>
  
  <p>Users are only allowed to be responsible for a project if they have a specific UserRole.
  This Userrole is name "responsible for projects" and has ID 2.</p>
  
  <p>In User model there are two has_many associations :responsible_assignments and responsible_projects. 
  This associations are only valid if the user has the UserRole "responsible for projects" with ID 2.</p>
  
  <p>Is it possible to create a conditional association in user model for responsible_* association and is this a common way to setup this kind of relations? </p>
  
  <p>What is the best practise to solve this kind of problems?</p>
  
  <pre><code>class Role &lt; ActiveRecord::Base
    has_many :assignments
    has_many :users, :through =&gt; :assignments
  
  class UserRole &lt; Role
  
  class ProjectRole &lt; Role
  
  class Assignment &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :role
    belongs_to :resource, :polymorphic =&gt; true
  
  class User &lt; ActiveRecord::Base
    has_many :assignments
    has_many :roles, :through =&gt; :assignments, 
                     :class_name =&gt; "UserRole"
    has_many :responsible_assignments, :class_name =&gt; "Assignment",
                                       :conditions =&gt; { :role_id =&gt; 4 }     // specific project role
    has_many :responsible_projects, :through =&gt; :responsible_assignments, 
                                   :source =&gt; :resource, 
                                   :source_type =&gt; 'Project',
                                   :conditions =&gt; { :status =&gt; 1 }          // project is active
    ...
  
  class Project &lt; ActiveRecord
    ...
  </code></pre>