Skip to content

Instantly share code, notes, and snippets.

@strika
Created October 28, 2016 07:42
Show Gist options
  • Save strika/a376432040eaa17247482d4824b96db2 to your computer and use it in GitHub Desktop.
Save strika/a376432040eaa17247482d4824b96db2 to your computer and use it in GitHub Desktop.
Preloading polymorphic associations
class Alert
belongs_to :alertable, polymorphic: true
belongs_to :incident, ->{ joins(:alerts).where(alerts: { id: Alert.where(alertable_type: "Incident") }) }, foreign_key: :alertable_id
belongs_to :action, ->{ joins(:alerts).where(alerts: { id: Alert.where(alertable_type: "Action") }) }, foreign_key: :alertable_id
end
Alert.includes(incident: :categories, action: :agencies)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment