I have a table with contacts, a table with events, and a joining table with contact_id and event_id to map their many-to-many relationship.
I’m new to RoR and I’m used to querying the first table, building a set of IDs and running a query on the second table where id IN setofids. Is that how its done in RoR as well?
Here’s what I have so far:
def view
@contact = Contact.find(params[:contact_id])
@contactevents = Contacts_Event.where(:contact_id => params[:contact_id])
s1 = Set.new
@contactevents.each do |contactevent|
s1.add(contactevent.event_id)
end
@contactevents_test = Event.where(:id => @s1)
end
well that is one way to do it but really you are looking at the
has_and_belongs_to_manyassociation (guide here). The idea is that when you do@contact = Contact.find(params[:contact_id])it will automatically pull all of the associatedContacts_Eventsas well with one query.