Ruby on rails 三种模型之间的权利关联
我正在努力寻找关联三个模型的最简单解决方案:Ruby on rails 三种模型之间的权利关联,ruby-on-rails,associations,Ruby On Rails,Associations,我正在努力寻找关联三个模型的最简单解决方案: 使用者 组织机构 角色 用户和组织是HABTM关联-一个用户可以有多个组织,反之亦然 一个用户也可以有多个角色,但每个组织只有一个角色 现在,我的模型中有: user.rb class User < ActiveRecord::Base has_many :roles, through: :organizations has_and_belongs_to_many :organizations, :join_table => :o
class User < ActiveRecord::Base
has_many :roles, through: :organizations
has_and_belongs_to_many :organizations, :join_table => :organizations_users
end
class Organization < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :organizations_users
has_many :roles
end
class Role < ActiveRecord::Base
has_many :users, through: :organizations
belongs_to :organizations
end
class User < ActiveRecord::Base
has_many :roles, through: :organizations_users
has_many :organizations, :through => :organizations_users
has_many :organizations_users
end
class Organization < ActiveRecord::Base
has_many :users, :through => :organizations_users
has_many :roles, :through => :organizations_users
has_many :organizations_users
end
class OrganizationUser < ActiveRecord::Base
belongs_to :user
belongs_to :organization
belongs_to :role
end
class Role < ActiveRecord::Base
end
class用户:组织\u用户
结束
organization.rb
class User < ActiveRecord::Base
has_many :roles, through: :organizations
has_and_belongs_to_many :organizations, :join_table => :organizations_users
end
class Organization < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :organizations_users
has_many :roles
end
class Role < ActiveRecord::Base
has_many :users, through: :organizations
belongs_to :organizations
end
class User < ActiveRecord::Base
has_many :roles, through: :organizations_users
has_many :organizations, :through => :organizations_users
has_many :organizations_users
end
class Organization < ActiveRecord::Base
has_many :users, :through => :organizations_users
has_many :roles, :through => :organizations_users
has_many :organizations_users
end
class OrganizationUser < ActiveRecord::Base
belongs_to :user
belongs_to :organization
belongs_to :role
end
class Role < ActiveRecord::Base
end
类组织:组织\u用户
有很多:角色
结束
角色.rb
class User < ActiveRecord::Base
has_many :roles, through: :organizations
has_and_belongs_to_many :organizations, :join_table => :organizations_users
end
class Organization < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :organizations_users
has_many :roles
end
class Role < ActiveRecord::Base
has_many :users, through: :organizations
belongs_to :organizations
end
class User < ActiveRecord::Base
has_many :roles, through: :organizations_users
has_many :organizations, :through => :organizations_users
has_many :organizations_users
end
class Organization < ActiveRecord::Base
has_many :users, :through => :organizations_users
has_many :roles, :through => :organizations_users
has_many :organizations_users
end
class OrganizationUser < ActiveRecord::Base
belongs_to :user
belongs_to :organization
belongs_to :role
end
class Role < ActiveRecord::Base
end
类角色
这有意义吗?以下是我的想法:
- 考虑到您正在使用的
以及Rails的默认值,您对has\u和\u属于\u many
join\u表的指定是多余的
- 只有在
表中既有组织
又有角色
字段的情况下,您的用户
才能工作,因为Rails希望对该表执行SQL才有多个:角色,通过::组织
,以查找这些字段选择
组织用户
模型中添加角色
字段,如下所示:
user.rb
class User < ActiveRecord::Base
has_many :roles, through: :organizations
has_and_belongs_to_many :organizations, :join_table => :organizations_users
end
class Organization < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :organizations_users
has_many :roles
end
class Role < ActiveRecord::Base
has_many :users, through: :organizations
belongs_to :organizations
end
class User < ActiveRecord::Base
has_many :roles, through: :organizations_users
has_many :organizations, :through => :organizations_users
has_many :organizations_users
end
class Organization < ActiveRecord::Base
has_many :users, :through => :organizations_users
has_many :roles, :through => :organizations_users
has_many :organizations_users
end
class OrganizationUser < ActiveRecord::Base
belongs_to :user
belongs_to :organization
belongs_to :role
end
class Role < ActiveRecord::Base
end
class用户:organizations\u用户
有很多:组织和用户
结束
organization.rb
class User < ActiveRecord::Base
has_many :roles, through: :organizations
has_and_belongs_to_many :organizations, :join_table => :organizations_users
end
class Organization < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :organizations_users
has_many :roles
end
class Role < ActiveRecord::Base
has_many :users, through: :organizations
belongs_to :organizations
end
class User < ActiveRecord::Base
has_many :roles, through: :organizations_users
has_many :organizations, :through => :organizations_users
has_many :organizations_users
end
class Organization < ActiveRecord::Base
has_many :users, :through => :organizations_users
has_many :roles, :through => :organizations_users
has_many :organizations_users
end
class OrganizationUser < ActiveRecord::Base
belongs_to :user
belongs_to :organization
belongs_to :role
end
class Role < ActiveRecord::Base
end
类组织:组织\u用户
拥有多个:角色,:至=>:organizations\u用户
有很多:组织和用户
结束
组织机构\u用户.rb
class User < ActiveRecord::Base
has_many :roles, through: :organizations
has_and_belongs_to_many :organizations, :join_table => :organizations_users
end
class Organization < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :organizations_users
has_many :roles
end
class Role < ActiveRecord::Base
has_many :users, through: :organizations
belongs_to :organizations
end
class User < ActiveRecord::Base
has_many :roles, through: :organizations_users
has_many :organizations, :through => :organizations_users
has_many :organizations_users
end
class Organization < ActiveRecord::Base
has_many :users, :through => :organizations_users
has_many :roles, :through => :organizations_users
has_many :organizations_users
end
class OrganizationUser < ActiveRecord::Base
belongs_to :user
belongs_to :organization
belongs_to :role
end
class Role < ActiveRecord::Base
end
class OrganizationUser
角色.rb
class User < ActiveRecord::Base
has_many :roles, through: :organizations
has_and_belongs_to_many :organizations, :join_table => :organizations_users
end
class Organization < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :organizations_users
has_many :roles
end
class Role < ActiveRecord::Base
has_many :users, through: :organizations
belongs_to :organizations
end
class User < ActiveRecord::Base
has_many :roles, through: :organizations_users
has_many :organizations, :through => :organizations_users
has_many :organizations_users
end
class Organization < ActiveRecord::Base
has_many :users, :through => :organizations_users
has_many :roles, :through => :organizations_users
has_many :organizations_users
end
class OrganizationUser < ActiveRecord::Base
belongs_to :user
belongs_to :organization
belongs_to :role
end
class Role < ActiveRecord::Base
end
类角色
以上假设您有某种理由希望
角色
继续成为活动模型
,而不仅仅是组织_用户
表中的字符串字段。完美,这就像一个符咒!只有一个问题-当我用种子数据填充数据库时,organizations_users表(现在有4列(id、user_id、organization_id、role_id)会为每个关联创建单独的条目,例如user 1-organization 1和user 1-role 1,而不是user 1-organization 1-role 1。有没有建议我如何解决这个问题?你是如何创建条目的?如果您曾经通过分配过除组织用户
之外的许多
帮助者方法,则会发生这种情况,因为其他帮助者方法只知道两个元素之间的关联。这很有意义。。。现在我正试图在控制台中使用以下代码创建一个新用户:OrganizationUserRole\create\u user(电子邮件:'thomas@aquarterit.com“,password:'password')
(我现在使用的是模型OrganizationUserRole,而不是用户OrganizationUser。无论如何,我应该选择更短的角色)。结果是=>OrganizationUserRole(id:integer,user\u id:integer,organization\u id:integer,role\u id:integer)
-我仍然在做一些错误的事情,但我只是不知道如何描述创建用户关联
方法(即在您的例子中是创建用户
)。它应该创建一个新的用户实例,并在调用create\u user
方法的对象中设置user\u id
的值。但是,#
在Ruby中是一个注释字符,因此在Rails控制台中,之后的所有内容都被忽略,因此您只需要获得OrganizationUserRole常量的打印输出。谢谢!你的回答为我指明了正确的方向。我按照这里的说明做了:它就像一个符咒。