title: Ruby/Rails 3: How do I use multiple nested includes in an AR where query?
body: <p>*<em>Edit:</em>*Last paragraph added to briefly describe the problem set--the data model is complex, but the problem set seems to dictate it. Maybe someone else has a better idea!</p>
  
  <p>I've been working on this problem for more than a month now as I have been learning Ruby on Rails, and I've yet to discover a resolution. I'm using Rails 3.09, Ruby 1.9.2, and PG SQL 8.0. I'm currently unable to upgrade to Rails 3.1 due to gem dependencies.</p>
  
  <p>I have a complex data model for a game that involves collecting rumors about activities of hirelings within the game and displaying the content of rumors to the end user. When a user selects a hireling, they should be able to view all rumors within the database where that hireling is either the actor or the target of the action the rumor is about. </p>
  
  <p>Here is the query in question:</p>
  
  <pre><code>@rumorEvents = current_webuser.player.rumor_events.includes(:rumor_instances =&gt;[:actor, :target]).where(
              "(hirelings.id=? AND ( (\"suspectedActor\"=hirelings.name) OR (\"suspectedTarget\"=hirelings.name)))", 
              session[:selectedHireling]).
              paginate(:page =&gt; params[:page], :per_page =&gt; 1).order('"rumor_instances".updated_at DESC')
  </code></pre>
  
  <p>As it stands above, this query returns all rumor events where session[:selectedHireling] is an actor, but does not return rumor events where it is a target. If I change
          includes(:rumor_instances =>[:actor, :target])
  to
          includes(:rumor_instances =>[:target])
  it will return all events where he is a target, but not an actor.</p>
  
  <p><strong>How can I return all rumor events for both actor and target?</strong></p>
  
  <p>Here are the relevant sections of the models:</p>
  
  <pre><code>class RumorEvent &lt; ActiveRecord::Base
  belongs_to :player
  has_many :rumor_event_rumor_instance_rels, :dependent =&gt; :destroy
  has_many :rumor_instances, :through =&gt; :rumor_event_rumor_instance_rels
      suspectedActor       :string(255)
      suspectedTarget      :string(255)
  
  
  class RumorInstance &lt; ActiveRecord::Base
  belongs_to :player
  belongs_to :game_event
  has_one :rumor_actor_rel, :dependent =&gt; :destroy
  has_one :actor, :through =&gt; :rumor_actor_rel, :source =&gt; :hireling
  has_one :rumor_target_rel, :dependent =&gt; :destroy
  has_one :target, :through =&gt; :rumor_target_rel, :source =&gt; :hireling
  
  class Hireling &lt; ActiveRecord::Base
  #  id                   :integer         not null, primary key
  #  name                 :string(255)
  </code></pre>
  
  <p>All rel models are basic, and known working. Here's an example:</p>
  
  <pre><code># Table name: rumor_event_rumor_instance_rels
  #
  #  id                :integer         not null, primary key
  #  rumor_event_id    :integer
  #  rumor_instance_id :integer
  #  created_at        :datetime
  #  updated_at        :datetime
  #
  
  class RumorEventRumorInstanceRel &lt; ActiveRecord::Base
  belongs_to :rumor_event
  belongs_to :rumor_instance
  end
  </code></pre>
  
  <p>Rumors about Game Events verbal description (from designer).</p>
  
  <p>When a game event occurs, 100% accurate and complete information is stored about that act, and kept available for 3 game days. As Hirelings and Players visit the cities near the event's location, they will be able to learn partials of information about the event (rumor instances), which range from "someone was assassinated" to "The Merchant of Venice, Sir Sellsalot was assassinated by Malagant, a hireling that serves (player) Mr. Brown". A player may collect multiple rumor instances about the same event from multiple sources, each with different levels of information, and different levels of accuracy (including in some cases false information).</p>
  
  <p>The code above illustrates the "top level" of information, which acts as a container for all information partials (rumor instances) about a particular event. </p>