I can't seem to get this right. My has many through relationship just isn't working. Here's the setup:
我似乎无法做到这一点。我有许多通过关系只是不工作。这是设置:
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
I've tried every variant of pluralization and just can't get past the unintialized error. What am I doing wrong? The table in the database (join model) is called groups_phone_numbers.
我已经尝试了复数化的每个变体,只是无法超越未初始化的错误。我究竟做错了什么?数据库中的表(连接模型)称为groups_phone_numbers。
Exact error (g is a group):
确切的错误(g是一组):
1.9.3p0 :002 > p g.phone_numbers
NameError: uninitialized constant Group::GroupsPhoneNumber
Migration that made the join table:
进行连接表的迁移:
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
Thanks
1 个解决方案
#1
-1
has_and_belongs_to_many could be the best choice for you, as long as you don't need GroupPhoneNumber actually. The code is like this:
has_and_belongs_to_many可能是你的最佳选择,只要你实际上不需要GroupPhoneNumber。代码是这样的:
class Group < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :phone_numbers
attr_accessible :name
end
class PhoneNumber < ActiveRecord::Base
belongs_to :user
has_many :responses
has_and_belongs_to_many :groups
attr_accessible :label, :number
end
class CreateGroupsPhoneNumbersJoinTable < ActiveRecord::Migration
def change
create_table(:groups_phone_numbers, :id => false) do |t|
t.integer :group_id
t.integer :phone_number_id
end
end
end
#1
-1
has_and_belongs_to_many could be the best choice for you, as long as you don't need GroupPhoneNumber actually. The code is like this:
has_and_belongs_to_many可能是你的最佳选择,只要你实际上不需要GroupPhoneNumber。代码是这样的:
class Group < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :phone_numbers
attr_accessible :name
end
class PhoneNumber < ActiveRecord::Base
belongs_to :user
has_many :responses
has_and_belongs_to_many :groups
attr_accessible :label, :number
end
class CreateGroupsPhoneNumbersJoinTable < ActiveRecord::Migration
def change
create_table(:groups_phone_numbers, :id => false) do |t|
t.integer :group_id
t.integer :phone_number_id
end
end
end