所以我不理解这个错误。我正在尝试执行以下操作:
module Xaaron
class GroupsRoles < ActiveRecord::Base
belongs_to :groups
belongs_to :roles
end
end
module Xaaron
class Group < ActiveRecord::Base
extend FriendlyId
friendly_id :group_name, use: [:slugged, :finders, :history]
has_many :groups_roles
has_many :roles, :through => :groups_roles
validates :group_name, presence: true, uniqueness: true
end
end
module Xaaron
class Role < ActiveRecord::Base
extend FriendlyId
friendly_id :role_name, use: [:slugged, :finders, :history]
has_many :groups_roles
has_many :groups, :through => :groups_roles
validates :role_name, presence: true
validates_uniqueness_of :role_name
end
end
它的基本原理我试图使用模型和关联来创建连接表,通过说角色有很多组,组有很多角色。这样我可以为许多组分配许多角色,反之亦然。
我还应该能够让角色所属的所有群组以及群组拥有的所有角色。
所以迁移:
class GroupsAndRoles < ActiveRecord::Migration
def change
create_table :xaaron_groups_roles do |t|
t.belongs_to :groups
t.belongs_to :roles
t.integer :group_id
t.integer :role_id
end
end
end
我有点困惑的事情之一,因为这是一个引擎 - 我应该引用xaaron_groups
表(就像那个叫表的那个),或者我可以{{ 1}}它知道我在说什么吗?
当我进入groups
并在创建群组后执行此操作。
rails c
我得到:g = Xaaron::Group.first
g.roles #=> I should get []
为什么?
答案 0 :(得分:0)
它正在寻找GroupsRoles(GroupsRole)的单数形式,而你的类以复数形式命名,因此无法找到它。 Rails会自动查找单数类名。
重命名你的课程,它应该有效。