Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot eagerly load the polymorphic association

I get the error Cannot eagerly load the polymorphic association :messageable_fromuser

online = Message.all.joins(:messageable_fromuser)

I tried

online = Message.all.includes(:messageable_fromuser)

But it doesn't include the joined table in the result. I see two queries in my log when using includes. I don't know why people recommend using includes to eager load. How is two queries going to join anything?

like image 946
Natus Drew Avatar asked May 14 '26 19:05

Natus Drew


1 Answers

Using preload should solve the error:

online = Message.preload(:messageable_fromuser)
like image 199
Alexander Avatar answered May 17 '26 07:05

Alexander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!