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 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?

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 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.