如何在 Rails 中关联 2 many_to_many 关系
How to associate 2 many_to_many relationship in Rails
如何关联双重many_to_many关系?还有,它叫什么?我知道没有“双重”many_to_many.
所以有这些模型在rails,一个User
,Role
,UserRole
,Menu
,RoleMenu
.
用户可以根据角色访问菜单。在控制台上,我可以这样做 User.first.roles.first.menus
。我的问题是有没有办法像这样 User.first.menus
,所以它会缩短?你如何将 User
与 Menu
联系起来?我应该在我的模型中添加什么?我应该创建什么迁移?
class User < ActiveRecord::Base
has_many :user_roles
has_many :roles, through: :user_roles
end
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.timestamps null: false
end
end
end
class Role < ActiveRecord::Base
has_many :user_roles
has_many :users, through: :user_roles
has_many :role_menus
has_many :menus, through: :role_menus
end
class CreateRoles < ActiveRecord::Migration
def change
create_table :roles do |t|
t.string :name
t.timestamps null: false
end
end
end
class UserRole < ActiveRecord::Base
belongs_to :user
belongs_to :role
end
class CreateUserRoles < ActiveRecord::Migration
def change
create_table :user_roles do |t|
t.belongs_to :user
t.belongs_to :role
t.timestamps null: false
end
end
end
class Menu < ActiveRecord::Base
has_many :role_menus
has_many :roles, through: :role_menus
end
class CreateMenus < ActiveRecord::Migration
def change
create_table :menus do |t|
t.string :name
t.timestamps null: false
end
end
end
class RoleMenu < ActiveRecord::Base
belongs_to :role
belongs_to :menu
end
class CreateRoleMenus < ActiveRecord::Migration
def change
create_table :role_menus do |t|
t.belongs_to :role
t.belongs_to :menu
t.timestamps null: false
end
end
end
class User < ActiveRecord::Base
has_many :user_roles
has_many :roles, through: :user_roles
has_many :menus, through: :roles
end
添加另一个 has_many.. 应该可行
您是指 User.first.menus
而不是 User.menus
吗?因为,后者无法实现,因为您试图通过 User
class(更像是 scope
实现)而不是特定用户访问 menus
。
对于第一种情况,据我所知,您已经知道has_many, through
关联。我们将使用相同的方法来实现这一目标。以下应该有效。
class User < ActiveRecord::Base
has_many :user_roles
has_many :roles, through: :user_roles
has_many :menus, through: :roles
end
How do you associate a double many_to_many relationship? and also, what is it called? I know that there's no "double" many_to_many.
嗯,是的,没有什么叫做双重多对多关联,但它更恰当地称为多重或嵌套多对多 relation/association。而上面说的,可以通过has_many, through
来实现
如何关联双重many_to_many关系?还有,它叫什么?我知道没有“双重”many_to_many.
所以有这些模型在rails,一个User
,Role
,UserRole
,Menu
,RoleMenu
.
用户可以根据角色访问菜单。在控制台上,我可以这样做 User.first.roles.first.menus
。我的问题是有没有办法像这样 User.first.menus
,所以它会缩短?你如何将 User
与 Menu
联系起来?我应该在我的模型中添加什么?我应该创建什么迁移?
class User < ActiveRecord::Base
has_many :user_roles
has_many :roles, through: :user_roles
end
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.timestamps null: false
end
end
end
class Role < ActiveRecord::Base
has_many :user_roles
has_many :users, through: :user_roles
has_many :role_menus
has_many :menus, through: :role_menus
end
class CreateRoles < ActiveRecord::Migration
def change
create_table :roles do |t|
t.string :name
t.timestamps null: false
end
end
end
class UserRole < ActiveRecord::Base
belongs_to :user
belongs_to :role
end
class CreateUserRoles < ActiveRecord::Migration
def change
create_table :user_roles do |t|
t.belongs_to :user
t.belongs_to :role
t.timestamps null: false
end
end
end
class Menu < ActiveRecord::Base
has_many :role_menus
has_many :roles, through: :role_menus
end
class CreateMenus < ActiveRecord::Migration
def change
create_table :menus do |t|
t.string :name
t.timestamps null: false
end
end
end
class RoleMenu < ActiveRecord::Base
belongs_to :role
belongs_to :menu
end
class CreateRoleMenus < ActiveRecord::Migration
def change
create_table :role_menus do |t|
t.belongs_to :role
t.belongs_to :menu
t.timestamps null: false
end
end
end
class User < ActiveRecord::Base
has_many :user_roles
has_many :roles, through: :user_roles
has_many :menus, through: :roles
end
添加另一个 has_many.. 应该可行
您是指 User.first.menus
而不是 User.menus
吗?因为,后者无法实现,因为您试图通过 User
class(更像是 scope
实现)而不是特定用户访问 menus
。
对于第一种情况,据我所知,您已经知道has_many, through
关联。我们将使用相同的方法来实现这一目标。以下应该有效。
class User < ActiveRecord::Base
has_many :user_roles
has_many :roles, through: :user_roles
has_many :menus, through: :roles
end
How do you associate a double many_to_many relationship? and also, what is it called? I know that there's no "double" many_to_many.
嗯,是的,没有什么叫做双重多对多关联,但它更恰当地称为多重或嵌套多对多 relation/association。而上面说的,可以通过has_many, through