我似乎无法做到这一点。我有很多通过关系只是不工作。这是设置:
class Group < ActiveRecord::Base
belongs_to :user
has_many :groups_phone_numbers, :dependent => :destroy
has_many :phone_numbers, through: :groups_phone_numbers
attr_accessible :name
end
class PhoneNumber < ActiveRecord::Base
belongs_to :user
has_many :responses
has_many :groups_phone_numbers
has_many :groups, through: :groups_phone_numbers
attr_accessible :label, :number
end
class GroupPhoneNumber < ActiveRecord::Base
belongs_to :group
belongs_to :phone_number
end
我已经尝试了复数的所有变体,但无法克服未初始化的错误。我究竟做错了什么?数据库中的表(连接模型)称为 groups_phone_numbers。
精确误差(g 是一个组):
1.9.3p0 :002 > p g.phone_numbers
NameError: uninitialized constant Group::GroupsPhoneNumber
生成连接表的迁移:
class CreateGroupPhoneNumbersJoinTable < ActiveRecord::Migration
def change
create_table(:groups_phone_numbers) do |t|
t.references :group
t.references :phone_number
t.timestamps
end
end
end
谢谢