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.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---