Sql 联接表中的重复行有三个:通过关联 描述
我有四种型号:Sql 联接表中的重复行有三个:通过关联 描述,sql,ruby-on-rails,ruby,Sql,Ruby On Rails,Ruby,我有四种型号: 使用者 组织机构 角色 组织用户角色 其思想是,一个用户可以属于多个组织,可以有多个角色,但每个组织只有一个 我的模型是这样的: user.rb class User < ActiveRecord::Base has_many :roles, :through => :organization_user_roles has_many :organizations, :through => :organization_user_roles has_ma
class User < ActiveRecord::Base
has_many :roles, :through => :organization_user_roles
has_many :organizations, :through => :organization_user_roles
has_many :organization_user_roles
end
class OrganizationUserRole < ActiveRecord::Base
has_many :organization_user_roles
has_many :users, :through => :organization_user_roles
has_many :roles, :through => :organization_user_roles
end
class Role < ActiveRecord::Base
end
class OrganizationUserRole < ActiveRecord::Base
belongs_to :user
belongs_to :organization
belongs_to :role
end
class用户:组织\u用户\u角色
拥有多个:组织,:通过=>:organization\u user\u角色
拥有多个:组织、用户、角色
结束
organization.rb
class User < ActiveRecord::Base
has_many :roles, :through => :organization_user_roles
has_many :organizations, :through => :organization_user_roles
has_many :organization_user_roles
end
class OrganizationUserRole < ActiveRecord::Base
has_many :organization_user_roles
has_many :users, :through => :organization_user_roles
has_many :roles, :through => :organization_user_roles
end
class Role < ActiveRecord::Base
end
class OrganizationUserRole < ActiveRecord::Base
belongs_to :user
belongs_to :organization
belongs_to :role
end
class OrganizationUserRole:组织\u用户\u角色
有多个:角色,:到=>:组织\u用户\u角色
结束
角色.rb
class User < ActiveRecord::Base
has_many :roles, :through => :organization_user_roles
has_many :organizations, :through => :organization_user_roles
has_many :organization_user_roles
end
class OrganizationUserRole < ActiveRecord::Base
has_many :organization_user_roles
has_many :users, :through => :organization_user_roles
has_many :roles, :through => :organization_user_roles
end
class Role < ActiveRecord::Base
end
class OrganizationUserRole < ActiveRecord::Base
belongs_to :user
belongs_to :organization
belongs_to :role
end
类角色
组织\用户\角色.rb
class User < ActiveRecord::Base
has_many :roles, :through => :organization_user_roles
has_many :organizations, :through => :organization_user_roles
has_many :organization_user_roles
end
class OrganizationUserRole < ActiveRecord::Base
has_many :organization_user_roles
has_many :users, :through => :organization_user_roles
has_many :roles, :through => :organization_user_roles
end
class Role < ActiveRecord::Base
end
class OrganizationUserRole < ActiveRecord::Base
belongs_to :user
belongs_to :organization
belongs_to :role
end
class OrganizationUserRole
我正在为我的db播种以下种子。rb
require 'faker'
# seed with standard roles
role_list = [
[ "superadmin" ],
[ "admin" ],
[ "user" ],
[ "owner" ],
]
role_list.each do |role|
Role.create( :name => role[0] )
end
# create default superadmin & organization
p = User.create(email: 'thomas@aquarterit.com', password: 'password')
o = Organization.create(name: 'A Quarter IT', website: 'www.aquarterit.com')
o.users << User.find_by_email('thomas@aquarterit.com')
p.roles << Role.find_by_name("superadmin")
# 30 organizations, 3 users each
30.times do |organization|
o = Organization.create(name: Faker::Company.name, website: Faker::Internet.domain_name)
3.times do |user|
p = User.create(email: Faker::Internet.email, password: 'password')
p.roles << Role.find_by_name("user")
o.users << User.last
end
end
require'faker'
#标准角色的种子
角色列表=[
[“超级管理员”],
[“管理”],
[“用户”],
[“所有者”],
]
角色列表。每个do |角色|
创建(:name=>角色[0])
结束
#创建默认超级管理员和组织
p=用户。创建(电子邮件:'thomas@aquarterit.com,密码:'password')
o=组织。创建(名称:“四分之一IT”,网站:“www.aquarterit.com”)
o、 用户您需要为三个参数添加一个数据库唯一键,如
add_index "organization_user_roles", ["user_id", "organization_id", "role_id"], name: "unique_roles", unique: true, using: :btree
然后在您的组织中\u用户\u角色模型
validates_uniqueness_of :role_id, scope: [:user_id, :organization_id]
我做了一个类似的应用程序,在我的数据库中有唯一的列,这个解决方案很有效,你需要3个表,请查看以下链接:
我最后按照这里的说明进行了操作:
它就像一个符咒