1

First things first i'm using Psql, If that helps, Probably doesnt matter.

I have two tables, (they have the model has_many inbetween)

# models
RegUser(id, Email, Name)
Booking(id , Event_id, Reg_user_id)

How do i go about linking these two together so that when viewing event 40, The registered users for that event will show, Its basically joining two two tables together from the params passed through (i believe!!!)

Heres the code i had working in the past

@reg_usr = RegUser.where(event_id: params[:id])

We moved the event_id to another table and dropped it out of the reguser table.

MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
Tester123
  • 219
  • 2
  • 15
  • Create a has_many relationship in your event model for reg_users, so something like: `has_many :reg_users, through: :bookings` – Doctor06 Nov 06 '15 at 16:34

1 Answers1

1

You should have the following relations:

class Event < ActiveRecord::Base
  has_many :bookings
  has_many :reg_users, through: :bookings

class RegUser < ActiveRecord::Base
  has_many :bookings
  has_many :events, through: :bookings

class Booking < ActiveRecord::Base
  belongs_to :event
  belongs_to :reg_user

And then you can simply do:

@event = Event.find(params[:id])
@reg_users = @event.reg_users # returns the reg_users associated to the Event
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
  • 1
    It may also be worth knowing that if you plan to enumerate over the records and use attributes from the reg_users, to prevent an N+1 issue use includes. – Polygon Pusher Nov 06 '15 at 17:00