gpt4 book ai didi

ruby-on-rails - 拥有并属于许多 3 型号

转载 作者:数据小太阳 更新时间:2023-10-29 07:54:08 25 4
gpt4 key购买 nike

如果我有 3 个模型要连接怎么办?

例如:

用户可以对许多不同的应用程序拥有许多不同的权限。

所以我需要一个表来存储:

user_id
permission_id
application_id

has_and_belongs_to_many 有可能吗?

谢谢

最佳答案

我会用 has_many :through 来完成。

class Upa < ActiveRecord::Base
belongs_to :user
belongs_to :permission
belongs_to :application
end
class User < ActiveRecord::Base
has_many :permissions, :through => :upas
has_many :applications, :through => :upas
end
class Permission < ActiveRecord::Base
has_many :users, :through => :upas
has_many :applications, :through => :upas
end
class Application < ActiveRecord::Base
has_many :permissions, :through => :upas
has_many :users, :through => :upas
end

examples of has_many :through

基本上,您可以在关系数据库中用经典的一对一、一对多和多对多关系描述的任何类型的关系都可以在 ActiveRecord 中描述。

关于ruby-on-rails - 拥有并属于许多 3 型号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6823951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com