lundi 31 août 2015

How to include many ActiveRecord relations after a join?

Trying to load multiple associations in one query:

GroupAssignment.where(groupable_type: 'Product').joins(:product, :packages).includes(:product)

The query works, but I would like to include the packages also. When I modify the query to:

GroupAssignment.where(groupable_type: 'Product').joins(:product, :packages).includes(:product, :packages)

I get the following error:

undefined method `gsub' for :groupable_id:Symbol

The models are defined as follows:

class GroupAssignment
  belongs_to :groupable, polymorphic: true
  # Defined specific relation to product
  # to make join query work
  belongs_to :product, foreign_key: 'groupable_id', conditions: "group_assignments.groupable_type = 'Product'"
  # Attempted this
  has_many :packages, through: :product
end

class Product
  has_many :packages
end

class Package
  belongs_to :product
end

Aucun commentaire:

Enregistrer un commentaire