RESTful has_many :through

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

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
@meeting.users << current_user


Okay so far, however I also need to set additional attributes on the
Attendee object: = “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 = => params[:meeting_id], :user_id
=>, rsvp => params[:rsvp])



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

Have a look at has_many :through…

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

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

class User < ActiveRecord::Base
has_many :attendances, :class_name => “Attendee”, :dependent =>
has_many :meetings, :through => : attendances

class Attendee < ActiveRecord::Base
belongs_to :meeting
belongs_to :user

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

class AttendeesController < ApplicationController
before_filter :require_login
before_filter :load_meeting

def create
@attendee = =>, :user_id =>, :rsvp => params[:rsvp])


def load_meeting
@meeting = Meeting.find(params[:meeting_id])

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 =

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs