bullet icon indicating copy to clipboard operation
bullet copied to clipboard

Not working for ActiveRecord references

Open tsball opened this issue 5 years ago • 0 comments

Below is my code, and it will raise the error as: USE eager loading detected ChargeStation => [:charge_station_groups] Add to your finder: :includes => [:charge_station_groups]

My Code:

chargers = Charger.includes(charge_station: :charge_station_groups).references(charge_station: :charge_station_groups)

chargers.map{|charger| charger.charge_station.charge_station_groups.map{ |group| {id: group.id, name: group.name} } }

Fixed: Remove the "references(charge_station: :charge_station_groups)" from my code.

Question? Sometime, we need to use the ActiveRecord's references replace with only using AR's includes. For example, if we need to fetch the chargers with large number, using"includes" for fetching chargers' charge_station and charge_station_groups may need the SQL as "where(charger_id: [.... too long uuids])". The SQL will be too long, so sometimes we prefer to using references to fetch the associations for this case.

Is it possible to ignore the "eager loading" error for references?

tsball avatar May 06 '19 03:05 tsball