Should I join a join table to another table? - ruby-on-rails

Let's say I am building a potluck organizing app. I have a join table tracking Participants and Events (MTM), I also have a Dishes table with dishes that are unique and associated to each Participant (OTM). Participants will sign up to events and agree to bring one or more of their Dishes.
The ParticipantEvents join table will track the RSVP but what is the best way to track which dishes the Participant is contributing to that Event?
Should I create a join table between the Dishes and the Events?
Should I create a join table between the ParticipantEvents join table and the Dishes table (ParticipantEventsDishes)?
Is there a better way?

The case looks like a three way (ternary) relationship to me. So I would create a three way join table, with three foreign keys.
Promised (DishID, EventID, ParticipantID)
Now you can join this with all three tables to produce the details for all participants. Note that a participant can promise more than one dish for an event.

Related

Sort by join table field

I have a users table and a reporting_events table where reporting_events belongs to user.
I want to sort the users table based on the reporting events values. Users should be sortable based on values of various reporting event names.
For example, there can be reporting events of two users with reporting event name as "avg_response_time" and reporting event values as 1 and 2 for the two users. We should be able to sort with avg_response_time of user both in desc and asc orders.
What I have tried:
reports = users.joins(:reporting_events).where("reporting_events.name='avg_response_time'").order("reporting_events.value desc")
filter_params[:sort_order] gives the column and direction it should sort by. When I don't use distict I'm getting duplicate users in users list.
I want to list all users even if reporting_events does't exist for the user.
You need a user an outer join instead on an inner join (default) :
users.joins('LEFT OUTER JOIN reporting_events ON reporting_events.user_id = users.id').where(...

How to ensure that join table (without model) data are unique

I'm trying to have read and unread status on a particular post by the specific user using Rails 5. I'm having post_users join table in which post_id and user_id will be stored. Posts and users associated with many to many (has and belongs to many). I don't have a model for the post_users table. How to check the join table in the controller?

how to get data from 2 rails join tables

i have a join table between alarms and lists, and have a join table between lists and cars, i want to find the alarm to be notified when a certain car is added in a certain list, i tried to use alarm.list.cars but that's doesn't work
any help please
Thanks in advance

Is there any way to join the Identity user table with any other table (Product) of our own in MVC 5

Is there any way to join the Identity user table with any other table of our own in MVC 5. Because fetching the data is really difficult. If we want to retrieve user product on the bases of some roles and then their products. So its a join of three tables but join of Products table with other two tables i don't know how to use this. Because User and Roles tables are identity table and belongs to IdentityDbContext.

Rails - Only pull in some HABTM associations on a case-by-case basis to avoid unnecessary joins

In Rails 4, I have a project in which I've set up three models with the following many-to-many relationships:
An Item
has_and_belongs_to_many categories
has_and_belongs_to_many tags
A Category
has_and_belongs_to_many items
A Tag
has_and_belongs_to_many items
And while it's easy to select an Item and automatically get all associated categories and tags, there are some situations in which I'd want to select items AND their associated categories, but NOT their tags. In these cases, I'd like to avoid doing extra database joins against the Tags table and ItemsTags join table. Can anyone help me with the correct find syntax to only join Items to categories? (Side note: I'm also planning on adding 10 additional many-to-many relationships between items and other models, but I'm just simplifying the scenario for this question. In the end, I'm trying to avoid doing a join with an excessive number of tables whenever I can.)
Thanks!
Rails will by default not load associated records unless you request it
Item.all will only fetch record from 'items' table
Then later in your code if you call item.categories that's the point when a query is performed to fetch all categories of this particular item. If you never call item.tags then the query to 'tags' table is never executed and the records are not fetch. Bottom line is: you can have as many associations as needed, as long as you don't explicitly call them they won't be loaded.
Side note about performance, rails offer several ways to join and include associated tables:
Item.include(:category).all Will trigger only 2 queries to fetch all items, and all associated categories.
Item.include(:category).joins(:category).all -> will trigger only 1 query joining the items and categories tables (but it may be slower than 2 requests)
So you have all control over what's loaded from the database. Those can apply for scope as well.

Resources