title: Ruby on Rails 3: Combine results from multiple has_many or has_many_through associations
body: <p>I have the following models. Users have UserActions, and one possible UserAction can be a ContactAction (UserAction is a polymorphism). There are other actions like LoginAction etc. So</p>
  
  <pre>
   class User &lt; AR::Base
    has_many :contact_requests, :class_name => "ContactAction"
    has_many :user_actions
    has_many_polymorphs :user_actionables, :from => [:contact_actions, ...], :through => :user_actions
   end
  
  class UserAction &lt; AR::Base
   belongs_to :user
   belongs_to :user_actionable, :polymorphic => true
  end
  
  class ContactAction &lt; AR::Base
   belongs_to :user
   named_scope :pending, ...
   named_scope :active, ...
  end
  </pre>
  
  <p>The idea is that a ContactAction joins two users (with other consequences within the app) and always has a receiving and a sending end. At the same time, a ContactAction can have different states, e.g. expired, pending, etc. </p>
  
  <p>I can say <code>@user.contact_actions.pending</code> or <code>@user.contact_requests.expired</code> to list all pending / expired requests a user has sent or received. This works fine.</p>
  
  <p>What I would now like is a way to <strong>join</strong> both types of ContactAction. I.e. <code>@user.contact_actions_or_requests</code>. I tried the following:</p>
  
  <pre>
  class User
  
   def contact_actions_or_requests
    self.contact_actions + self.contact_requests
   end
  
   # or
   has_many :contact_actions_or_requests, :finder_sql => ..., :counter_sql => ...
  
  end
  </pre>
  
  <p>but all of these have the problem that it is not possible to use additional finders or named_scopes on top of the association, e.g. <code>@user.contact_actions_or_requests.find(...)</code> or <code>@user.contact_actions_or_requests.expired</code>.</p>
  
  <p>Basically, I need a way to express a 1:n association which has two different paths. One is <code>User -> ContactAction.user_id</code>, the other is <code>User -> UserAction.user_id -> UserAction.user_actionable_id -> ContactAction.id</code>. And then join the results (ContactActions) in one single list for further processing with named_scopes and/or finders.</p>
  
  <p>Since I need this association in literally dozens of places, it would be a major hassle to write (and maintain!) custom SQL for every case. </p>
  
  <p>I would prefer to solve this in Rails, but I am also open to other suggestions (e.g. a PostgreSQL 8.3 procedure or something simliar). The important thing is that in the end, I can use Rails's convenience functions like with any other association, and more importantly, also nest them. </p>
  
  <p>Any ideas would be very much appreciated.</p>
  
  <p>Thank you!</p>
  
  <hr>
  
  <p>To provide a sort-of answer to my own question:</p>
  
  <p>I will probably solve this using a database view and add appropriate associations as needed. For the above, I can </p>
  
  <ul>
  <li>use the SQL in finder_sql to create the view, </li>
  <li>name it "contact_actions_or_requests", </li>
  <li>modify the SELECT clause to add a user_id column, </li>
  <li>add a app/models/ContactActionsOrRequests.rb,</li>
  <li>and then add "has_many :contact_actions_or_requests" to user.rb.</li>
  </ul>
  
  <p>I don't know how I'll handle updating records yet - this seems not to be possible with a view - but maybe this is a first start.</p>