# RESTful has\_many :through

**URL:** <https://www.ruby-forum.com/t/restful-has-many-through/174825>\
**Category:** Rails\
**Created:** [September 2, 2009, 7:15pm UTC](https://www.ruby-forum.com/t/restful-has-many-through/174825 "2009-09-02T19:15:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [September 2, 2009, 7:15pm UTC](https://www.ruby-forum.com/t/restful-has-many-through/174825/1 "2009-09-02T19:15:18Z")

</div>

Okay, I think I get the concept of a RESTful many-to-many association  
controller, but I’ve hit a bit of a wall actually implementing it. I’ve  
been Googling for a while, but this simple question is starting to take  
up too much time:

Heres where I am:

I have a Meeting model related to Users through Attendees. Simple enough  
so far. I need to be able to create, update and destroy meeting  
attendees:

in the controller I, of course have create, update and destroy actions  
and the attendees are nested under meetings giving the following route:  
POST /meetings/1/attendees (for create action).

def create  
@meeting = Meeting.find(params[:meeting\_id]) # actually in  
before\_filter  
@meeting.users \<\< current\_user  
…  
…  
end

Okay so far, however I also need to set additional attributes on the  
Attendee object:

attendee.rsvp = “Yes” # for example

So rather than using \<\< to associate the logged in user directly to the  
meeting should I instead create the Attendee model directly set the  
attributes and save?

def create  
attendee = Attendee.new(:meeting\_id =\> params[:meeting\_id], :user\_id  
=\> current\_user.id, rsvp =\> params[:rsvp])  
if attendee.save  
…  
else  
…  
end  
end

Or am I way off base and there’s a smart and simple way to accomplish  
this?

---

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [September 2, 2009, 9:21pm UTC](https://www.ruby-forum.com/t/restful-has-many-through/174825/2 "2009-09-02T21:21:26Z")

</div>

Have a look at has\_many :through…

I would see this to be a meeting has\_many :users :through attenders/  
attendees or maybe attendance

---

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [September 2, 2009, 9:50pm UTC](https://www.ruby-forum.com/t/restful-has-many-through/174825/3 "2009-09-02T21:50:42Z")

</div>

heimdull wrote:

> Have a look at has\_many :through…
> 
> I would see this to be a meeting has\_many :users :through attenders/  
> attendees or maybe attendance

The model I understand and that all works fine. I actually have it  
modeled like so:

class Meeting \< ActiveRecord::Base  
has\_many :attendees, :dependent =\> :destroy  
has\_many :users, :through =\> :attendees  
end

class User \< ActiveRecord::Base  
has\_many :attendances, :class\_name =\> “Attendee”, :dependent =\>  
:destroy  
has\_many :meetings, :through =\> : attendances  
end

class Attendee \< ActiveRecord::Base  
belongs\_to :meeting  
belongs\_to :user  
end

It’s the controller create and update methods that are tripping me up. I  
was just trying to find out how the “experts” handled many-to-many  
associations in controllers. I actually end up using something like  
this:

class AttendeesController \< ApplicationController  
before\_filter :require\_login  
before\_filter :load\_meeting

def create  
@attendee = Attendee.new(:meeting\_id =\> @meeting.id, :user\_id =\>  
current\_user.id, :rsvp =\> params[:rsvp])  
…  
end

protected  
def load\_meeting  
@meeting = Meeting.find(params[:meeting\_id])  
end  
end

I’m guessing that’s the most straight forward way to do this? I’m not  
exactly sure if setting the foreign keys directly like that is best  
practice, but it I haven’t figured out a clean way to handle this using  
the associations.

Adding the user to the meeting would create the association  
(@meeting.users \<\< current\_user), but then I wouldn’t have access to the  
attendee object in order to set the extra “rsvp” attribute. I’d have to  
go back and fetch it something like @attendee =  
@meeting.attendees.find\_by\_user\_id(current\_user.id).

So this is why I was hoping to get some insight from the community.
