jeudi 18 juin 2015

has_and_belongs_to_many loads wrong resource

I have 3 models

# generic one
class Someone < ActiveRecord::Base
end

# customer
class Customer < Someone
  has_and_belongs_to_many :groups, join_table: "some_join_table", class_name: "Group"
end

# custom group
class Group < GenericGroup
  has_and_belongs_to_many :customers, join_table: 'some_join_table', class_name: "Customer"
end

Let's assume that database is feeded.

When i execute command Customer.first.groups I'll get niece array with correct results ([]). When I try to execute same on Someone model nothing will happen but when i try do this reverse 'magic happens'

Group.first.customers
#=> [<Someone..>]

How can i force has_and_belongs_to_many to return right version of Customer class?

Aucun commentaire:

Enregistrer un commentaire