Note that there are some explanatory texts on larger screens.

plurals
  1. PORails - retrieving data across 3 tables in has many through
    text
    copied!<p>I have a basic event management app in rails 3 - users (model: user) can create events (model: event) as hosts (:event belongs_to user). Other users can attend the events (:user has_many :attending, :through => :attendances, :source => :event). All of these models and associations are working fine (a good start).</p> <pre><code># the user model class User &lt; ActiveRecord::Base has_many :events, :dependent =&gt; :destroy has_many :attendances, :foreign_key =&gt; "attendee_id", :dependent =&gt; :destroy has_many :attending, :through =&gt; :attendances, :source =&gt; :event end # the events which users create class Event &lt; ActiveRecord::Base belongs_to :user has_many :attendances, :foreign_key =&gt; "event_id",:dependent =&gt; :destroy has_many :guests, :through =&gt; :attendances, :source =&gt;:user end # the join model for attendances class Attendance &lt; ActiveRecord::Base belongs_to :attendee, :class_name =&gt; "User" #edit: addedin :class_name... after comment belongs_to :event, :class_name =&gt; "Event" end </code></pre> <p>Now the problem: I have a 'manage events' page where the host should be able to see all the requested attendances to his events, and accept or decline their attendance. I can get the list of events which are hosted by the user (&lt;%= render @user.events %>) but cant seem to work out how to bring back the list of all the users who have requested attendance at the hosts' events (to allow the host to accept or decline guests). </p> <pre><code>&lt;html&gt; &lt;% unless @user.events.empty? %&gt; #code on the manage events page &lt;table class="events" summary="User Events"&gt; &lt;%= render @user.events %&gt; &lt;/table&gt; &lt;% end %&gt; &lt;/html&gt; </code></pre> <p>I'm sure the solution is simple, but 2 days of playing around and im getting nowhere quickly. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload