Reinier Reinier - 1 year ago 71
Ruby Question

Join with where clause that filters out specific records, rails active record

I am making a sports application with leagues and events, the events contain a

, the leagues have a
and an
. How can I get all the event with starting date today together with the league name and country.

I tried:

League.joins(:events).where(events: {event_start:})

But this returns all the leagues that have an event today, when I then try to access the events from that specific league, all events are included and not only the ones that are being plaid today.

Answer Source

Start with Events and include relevant League to avoid N+1 problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download