-
-
Save pduersteler/6906667 to your computer and use it in GitHub Desktop.
This file contains 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
# Aim: load all activities having the same customer. | |
# | |
# PublicActivity::Activity(id: integer, trackable_id: integer, trackable_type: string, ...) | |
# => belongs_to :trackable, polymorphic: true | |
# => trackable is either Project or Service | |
PublicActivity::Activity.joins(:trackable) | |
# ActiveRecord::EagerLoadPolymorphicError: Can not eagerly load the polymorphic association :trackable | |
PublicActivity::Activity.includes(:trackable) | |
# PublicActivity::Activity Load (0.3ms) SELECT "activities".* FROM "activities" | |
# Project Load (0.2ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" IN (1) | |
# Mandate Load (0.2ms) SELECT "mandates".* FROM "mandates" WHERE "mandates"."id" IN (1) | |
PublicActivity::Activity.includes(trackable: :customer) | |
# PublicActivity::Activity Load (0.3ms) SELECT "activities".* FROM "activities" | |
# Project Load (0.3ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" IN (1) | |
# Mandate Load (0.3ms) SELECT "mandates".* FROM "mandates" WHERE "mandates"."id" IN (1) | |
# Customer Load (0.4ms) SELECT "customers".* FROM "customers" WHERE "customers"."id" IN (1) | |
# Customer Load (0.4ms) SELECT "customers".* FROM "customers" WHERE "customers"."id" IN (1) | |
PublicActivity::Activity.includes(trackable: :customer).where('customers.id = 1').references(:customers) | |
# ActiveRecord::EagerLoadPolymorphicError: Can not eagerly load the polymorphic association :trackable |
This file contains 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 Customer < ActiveRecord::Base | |
has_many :projects | |
has_many :services | |
end | |
class Project < ActiveRecord::Base | |
include PublicActivity::Common | |
belongs_to :customer | |
end | |
class Service < ActiveRecord::Base | |
include PublicActivity::Common | |
belongs_to :customer | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment