Rails belongs_to 和 has_many 同一型号
Rails belongs_to and has_many of the same model
我正在尝试在两个模型 Users 和 Circles 之间的 rails 中创建数据库模式。圆圈是用户定义的组,只有创建它的用户知道。圈子包含用户选择加入该圈子的其他用户和圈子名称。
所以我的解决方案如下:
- 用户模型:has_many:圈子
- 圈子模型:belongs_to:用户,has_many:用户
我知道存在 has_many through 方法,但我不知道这对我的情况是否必要。
你实际上需要两个不同的协会。第一种是一对多关联。懒惰的设置方式是:
class Circle < ApplicationRecord
belongs_to :user
end
class User < ApplicationRecord
has_many :circles
end
这通过圈子上的外键列 user_id
将用户链接到圈子。但是它非常模棱两可 - user.circles 是什么意思?是用户创建的圈子还是他所属的圈子?即使需要一些配置,也最好更明确一点:
class RenameUserToCreator < ActiveRecord::Migration[6.0]
def change
rename_column :circles, :user_id, :creator_id
end
end
# rails g model circle
class Circle < ApplicationRecord
belongs_to :creator, class_name: 'User'
end
class User < ApplicationRecord
has_many :created_circles,
class_name: 'Circle',
foreign_key: :creator_id
end
接下来您要向圈子中添加成员。这是一个多对多关联,可以使用 has_many through:
或 has_or_belongs_to_many
来完成。两者都使用连接 table 但 has_or_belongs_to_many
没有模型并且其实际用途非常有限。在命名 join tables 时,懒惰的约定是只使用 a 和 b 的合并 - CircleUser
但如果您能想到适合该域的名称,请使用更好的名称。
class Circle
belongs_to :creator, class_name: 'User'
has_many :memberships
has_many :users, through: :memberships
end
# rails g model membership user:belongs_to circle:belongs_to
class Membership
belongs_to :user
belongs_to :circle
end
class User
has_many :created_circles,
class_name: 'Circle',
foreign_key: :creator_id
has_many :memberships
has_many :circles, through: :memberships
end
要记住的一件事是每个协会都必须有一个唯一的名称。如果我们没有完成上一步并写道:
class User
has_many :circles
has_many :memberships
has_many :circles, through: :memberships
end
后一种关联只会破坏前一种关联。
我正在尝试在两个模型 Users 和 Circles 之间的 rails 中创建数据库模式。圆圈是用户定义的组,只有创建它的用户知道。圈子包含用户选择加入该圈子的其他用户和圈子名称。
所以我的解决方案如下:
- 用户模型:has_many:圈子
- 圈子模型:belongs_to:用户,has_many:用户
我知道存在 has_many through 方法,但我不知道这对我的情况是否必要。
你实际上需要两个不同的协会。第一种是一对多关联。懒惰的设置方式是:
class Circle < ApplicationRecord
belongs_to :user
end
class User < ApplicationRecord
has_many :circles
end
这通过圈子上的外键列 user_id
将用户链接到圈子。但是它非常模棱两可 - user.circles 是什么意思?是用户创建的圈子还是他所属的圈子?即使需要一些配置,也最好更明确一点:
class RenameUserToCreator < ActiveRecord::Migration[6.0]
def change
rename_column :circles, :user_id, :creator_id
end
end
# rails g model circle
class Circle < ApplicationRecord
belongs_to :creator, class_name: 'User'
end
class User < ApplicationRecord
has_many :created_circles,
class_name: 'Circle',
foreign_key: :creator_id
end
接下来您要向圈子中添加成员。这是一个多对多关联,可以使用 has_many through:
或 has_or_belongs_to_many
来完成。两者都使用连接 table 但 has_or_belongs_to_many
没有模型并且其实际用途非常有限。在命名 join tables 时,懒惰的约定是只使用 a 和 b 的合并 - CircleUser
但如果您能想到适合该域的名称,请使用更好的名称。
class Circle
belongs_to :creator, class_name: 'User'
has_many :memberships
has_many :users, through: :memberships
end
# rails g model membership user:belongs_to circle:belongs_to
class Membership
belongs_to :user
belongs_to :circle
end
class User
has_many :created_circles,
class_name: 'Circle',
foreign_key: :creator_id
has_many :memberships
has_many :circles, through: :memberships
end
要记住的一件事是每个协会都必须有一个唯一的名称。如果我们没有完成上一步并写道:
class User
has_many :circles
has_many :memberships
has_many :circles, through: :memberships
end
后一种关联只会破坏前一种关联。