Created
October 28, 2016 07:42
-
-
Save strika/a376432040eaa17247482d4824b96db2 to your computer and use it in GitHub Desktop.
Preloading polymorphic associations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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