I am trying to get the first report whose alerts belongs to alert_code
whose name is "inside virtual fence". So there are 3 associations
here. Trying to establish the connection.
I think that you’re looking for ‘includes’ rather than ‘joins’ for these associations.
Report.includes({:alerts => :alert_code}).
To use joins, you would have to supply more SQL-ish clauses, not symbols for the associations.
joins accepts either association names or sql clauses. When passing association names the syntax is the same as for includes, in particular you have to specify nested associations as a hash (as you have done).