i trying create 2 has many relations same destination tables unable override default method name rails creates when defining association.
the following case:
class user < applicationrecord has_many :conference_attendees, dependent: :destroy has_many :conference_organizers, dependent: :destroy has_many :conferences, through: :conference_attendees, class_name: 'attending', dependent: :destroy has_many :conferences, through: :conference_organizers, source: :conference, dependent: :destroy class conferenceorganizer < applicationrecord alias_attribute :organizers, :users belongs_to :conference belongs_to :user end class conferenceattendee < applicationrecord belongs_to :conference belongs_to :user end class conference < applicationrecord has_many :conference_attendees, dependent: :destroy has_many :conference_organizers, dependent: :destroy has_many :users, through: :conference_attendees, dependent: :destroy has_many :organizers, through: :conference_organizers, source: :user, dependent: :destroy
i trying access conferences user attended , conferences user organized using following:
user.find(id: <id>).organizing user.find(id: <id>).attending
but unable ,
user.find(id: <id>).conferences
defaults organizing conferences. how access attending conferences?
since doing join on same 2 tables might cleaner 1 join table , add field called status distinguish organizer attendee. way can add other status values such "presenter".
set table rails g model registration user:references conference:references status:string
; rake db:migrate
set model associations:
# app/models/user.rb has_many :registrations, dependent: :destroy # app/models/conference.rb has_many :registrations, dependent: :destroy # app/models/registration.rb belongs_to :user belongs_to :conference scope :attendees, -> { where( status: "attendee") } scope :organizers, -> { where( status: "organizer") }
then show attendees , organizers on conference show page:
# app/controllers/conferences_controller.rb def show @conference = conference.find(params[:id]) @attendee_registrations = @conference.registrations.attendees @organizer_registrations = @conference.registrations.organizers end # app/views/conferences/show.html.erb <h3>organizers</h3> <% @organizer_registrations.each |registration| %> <li><%= link_to(registration.user.name, registration.user) %></li> <% end %> <h3>attendees</h3> <% @attendee_registrations.each |registration| %> <li><%= link_to(registration.user.name, registration.user) %></li> <% end %>
then show conferences on user's show page:
# app/controllers/users_controller.rb def show @user = user.find(params[:id]) @attending_registrations = @user.registrations.attendees @organizing_registrations = @user.registrations.organizers end # app/views/users/show.html.erb <h3>conferences organizing</h3> <% @organizing_registrations.each |registration| %> <li><%= link_to(registration.conference.name, registration.conference) %><br> date: <%= registration.conference.date %></li> <% end %> <h3>conferences attending</h3> <% @attending_registrations.each |registration| %> <li><%= link_to(registration.conference.name, registration.conference) %><br> date: <%= registration.conference.date %></li> <% end %>
Comments
Post a Comment