has_and_belongs_to_many 加载了错误的资源

has_and_belongs_to_many loads wrong resource

我有 3 个模型

# 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

让我们假设数据库已被馈送。

当我执行命令 Customer.first.groups 时,我将得到具有正确结果 ([]) 的 niece 数组。当我尝试在 Someone 模型上执行相同的操作时,什么也不会发生,但是当我尝试执行相反的操作时 'magic happens'

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

如何强制 has_and_belongs_to_many 到 return 正确版本的客户 class?

我找到了这篇博文 http://blog.flatironschool.com/why-you-dont-need-has-and-belongs-to-many/ 并决定将 HABTM 的关系更改为 has_many through。更改后一切正常。