Hi,
New rails developer here trying to learn my way around active record associations.
I'm building an event scheduling application and am working on the invitation feature. I already have a basic when / where event creation form and I now want to make a way for the event creator to generate a list of users who are invitees to the event from a list of the creator's friends on the site (friends are recorded by a reflexive has_many :through friendship relationship on the User model) . I have set up a has_many :through invitation model to represent the many to many relatiionship between events and users (invitees). The respective models look like this:
User.rb
class User < ActiveRecord::Base
has_many :friendships has_many :friends, :through => :friendships
attr_accessor :password
attr_accessible :first_name, :middle_name, :last_name, :email, :zipcode, :password, :password_confirmation
has_many :events
has_many :invitations has_many :events, :through => :invitations
end
Event.rb
class Event < ActiveRecord::Base
attr_accessible :title, :description, :location, :time_start, :time_end, :event_access_type
belongs_to :user
has_many :invitations has_many :invitees, :through => :invitations
accepts_nested_attributes_for :invitations
end
Friendship.rb
class Friendship < ActiveRecord::Base
belongs_to :user belongs_to :friend, :class_name => "User"
end
Invitation.rb
class Invitation < ActiveRecord::Base
belongs_to :event belongs_to :invitee, :class_name => "User"
end
What I'm trying to with the form is generate a drop down list with the multiple selection enabled (terrible UI I know, but this is just for testing and prototyping purposes) that is populated with the current user's friends on the site. In order to this I am currently using this form helper method:
<%= collection_select(:friendship, :friend_id, current_user.friendships , :friend_id, :friends_name, {}, html_options = {:multiple => true}) %>
this generates a drop down which I want to submit the friend_ids (i.e. user_ids) of the users that the event creator wants to invite. My next step is to take these selections upon submission and use each of the friend_id values to then generate a new invitation association between the created event and the friend. My understanding is that since this a multi-model form I'll need to include "accepts_nested_attributes_for :invitations" in the event model. My basic problem is I'm not clear on what the form and controller syntax should be to take the submitted drop down values and use them to generate new invitation objects. Here is my latest attempt at the form view:
_event.form.html.erb
<%= form_for(@event) do |f| %> <%= render 'shared/error_messages', :object => f.object %> <div class="field"> <%= f.label :title %><br /> <%= f.text_field :title %> </div> <div class="field"> <%= f.label :description %><br /> <%= f.text_area :description %> </div> <div class="field"> <%= f.label :location %><br /> <%= f.text_field :location %> </div> <div class="field"> <%= f.label "Event type" %><br /> <%= select( "event", "event_access_type", {"" => "", "Public" => "public", "Friends of Friends" => "friends_of_friends", "Private (Invite Only)" => "private"}) %> </div> <% f.fields_for :invitation do |invitation_form| %> <div class="field"> <%= invitation_form.label "Who's invited?" %><br /> <%= collection_select(:friendship, :friend_id, current_user.friendships , :friend_id, :friends_name, {}, html_options = {:multiple => true}) %> </div> <% end %> <div class="actions"> <%= f.submit "Submit" %> </div> <% end %>
Would greatly appreciate it if someone could offer their guidance on how this would be done and if I am making a many mistakes in terms of the relations or form methods I am using.
Thanks!