I have seen some related questions on my issue, but nothing that quite covers exactly what I need. We have 3 database tables that are splitting data on booths that I need accessed in our javascript. I will be pulling this data to convert into an array of javascript objects. The issue I have is trying to perform the join between these 3 tables, then pushing certain values into an object in an efficient manner that won't be too resource-draining. My 3 objects are outlined here. I am not very familiar with RoR still, so I wrote out what my query would look like in SQL first:
SELECT b.BoothNumber, bt.Width, bt.Depth, bl.room_id, bl.x, bl.y
FROM Booths b
LEFT JOIN booth_types bt ON bt.id = b.booth_type_id
JOIN booth_locations bl ON bl.id = b.booth_location_id
WHERE bl.room_id = @room_ID_provided_by_javascript
I can do the last line in the JS, but I included just for clarity. What is my best option? My first attempt looks like the below code. Of course, it doesn't work, because room_id/etc. does not belong to the 'booth' object.
<% Booth.includes(:booth_location, :booth_type).find_each do |booth| %>
if(roomIds.indexOf(<%= booth.room_id%>) > -1){//If index in roomIds (is this room in the event?)
booths.push({boothId: <%= booth.id %>, roomId: <%= booth.roomId %>, x: <%= booth.x %>, y: <%= booth.y %>,
boothNumber: <%= booth.boothNumber %>, companyId: <%= booth.companyId %>, width: <%= booth.width %>, depth: <%= booth.depth %>});
}
<% end %>
I have the basic belongs_to
and has_many
connections between these tables in my controllers, but nothing more. I understand I need to include some sort of use of the through
keyword in my controllers, but I don't really know how those are supposed to align with how my database is setup. I've seen answers vary from adding 20+ lines to each controller to just writing an extra few words on my opening Ruby line. What will work for me?