0

PLEASE HELP! REMOVE DUPLICATE STATUS!

I know this was marked as a duplicate, but I do not think the other postings address my issue, please help explain what I am missing or address my current issue.

Thank you!

SOLUTION NOT RESOLVED FROM OTHER POSTINGS

Based on this Eager load polymorphic I tried

Listing Model

  belongs_to :listable, polymorphic: true
  belongs_to :car, foreign_type: 'Car', foreign_key: 'listable_id'
  belongs_to :truck, foreign_type: 'Truck', foreign_key: 'listable_id'
  belongs_to :bike, foreign_type: 'Bike', foreign_key: 'listable_id'

AND TRIED:

Listing Model

  belongs_to :listable, polymorphic: true
  belongs_to :car, -> { where(listings: {listable_type: 'Car'}) }, foreign_key: 'listable_id'

But got the same error!

This post shows that it should work: https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Eager+loading+of+associations

But it does not.

ORIGINAL POST:

Need to query all listings that belong to a user in Rails 5.2.

I have read many other posts, but do not see how they apply to this issue.

I tried

Listing.includes(listable:[:user]).where('users.id': 100)

But I get

ActiveRecord::EagerLoadPolymorphicError (Cannot eagerly load the polymorphic association :listable)

User Model

User has_one Car
User has_one Truck
User has_one Bike

Car Model

belongs_to :user
has_one  :listing, as: :listable

Truck Model

belongs_to :user
has_one  :listing, as: :listable

Bike Model

belongs_to :user
has_one  :listing, as: :listable

Listing Model

belongs_to :listable, polymorphic: true
user2012677
  • 5,465
  • 6
  • 51
  • 113
  • @mrzasa, I saw that post, but do not think it solve the issue. Am I missing something? – user2012677 Jul 25 '19 at 20:29
  • @battleTested Please let me know what I am missing from the other post, as I do not seem to understand why people think this is a duplicate. – user2012677 Jul 26 '19 at 14:03

0 Answers0