Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUBY

eager load polymorphic rails

You need to add a custom belongs_to to your polymorphic model

class Shipment
  has_many :payments, as: :paymentable
end

class Payment  
  belongs_to :paymentable, polymorphic: true
  # Add a custom belongs_to that points to Shipment
  belongs_to :shipment, foreign_type: 'Shipment', foreign_key: 'paymentable_id'
end
 
PREVIOUS NEXT
Tagged: #eager #load #polymorphic #rails
ADD COMMENT
Topic
Name
1+1 =