gpt4 book ai didi

ruby-on-rails - Rails - 3 列关联表?

转载 作者:行者123 更新时间:2023-12-04 18:15:21 25 4
gpt4 key购买 nike

我目前正在尝试在 Rails 中创建一个相当简单的关联,但事实证明它比我想象的要困难。

我有 3 个模型,用户、经销商和角色。用户/经销商是多对多的。用户可以有许多角色,但他们应该特定于经销商 - 即用户可以在一个经销商处担任经理和主管,在另一个经销商处担任经理,在另一个经销商处担任主席。

我正在寻找的结果是我将能够做到user.dealer.roles并且(例如对于第一个经销商)它将返回经理和董事。如果我这样做 dealer.user.roles. 也会发生同样的情况

My classes look as follows:



class User  :dealer_users
has_many :dealer_user_roles, :through => :dealer_users
has_many :roles, :through => :dealer_user_roles
end

class Dealer :dealer_users
has_many :dealer_user_roles, :through => :dealer_users
has_many :roles, :through => :dealer_user_roles
end

class Role :dealer_user_roles
has_many :users, :through => :dealer_users
end

表格看起来像(绿色表格):

Model diagram

我正在尝试使用以下代码为数据库播种:
dealer = Dealer.where(name: Faker::Company.name).first_or_create
user = User.where(email: Faker::Internet.email).first_or_create
user.first_name = Faker::Name.first_name
user.last_name = Faker::Name.last_name
user.password = 'password'
user.password_confirmation = 'password'
dealer.users user.id).first.roles 1 + Random.rand(4))

这会导致错误,即:“无法修改关联 'User#roles',因为它通过多个其他关联。”

谁能帮助我告诉我哪里出错了,以及我该如何解决?

最佳答案

在我的脑海中,我将其建模为:

# Table name: users
# id :integer not null, primary key
# name :string
class User
has_many :employments
has_many :dealers, :through => :employments
has_many :roles, :through => :employments
end

# Table name: dealers
# id :integer not null, primary key
# name :string
class Dealer
has_many :employments
has_many :users, :through => :employments
has_many :roles, :through => :employments
end

# Table name: employments
# id :integer not null, primary key
# user_id :integer not null
# dealer_id :integer not null
class Employment
belongs_to :dealer, :inverse_of => :employments
belongs_to :user, :inverse_of => :employments
has_many :roles
scope :for_user, lambda{ |user| where(:user_id => user.id) }
scope :for_dealer, lambda{ |dealer| where(:dealer_id => dealer.id) }
end

# Table name: roles
# id :integer not null, primary key
# employment_id :integer not null
# name :string not null
class Role
belongs_to :employment
scope :at_dealership, lambda{ |dealer| joins(:employment).where(:dealer_id => dealer.id) }
scope :for_employee, lambda{ |employee| joins(:employment).where(:user_id => employee.id) }
# alternately:
# scope :at_dealership, lambda{ |dealer| joins(:employment).where('employments.dealer_id = ?', dealer.id) }
# scope :for_employee, lambda{ |employee| joins(:employment).where('employments.user_id = ?',employee.id) }
end

这使得处理更容易,并且也将角色放在他们实际所属的位置 - 我称之为就业的用户/经销商的组合。

然后你可以说
dealer.users.first.roles 
# or
user.dealers.where('some condition').roles

编辑:使用范围,所有这些都应该做你想要的。
user = User.first
dealer = user.dealers.first

user.roles.at_dealership(dealer)

dealer.roles.for_employee(user)

dealer.employments.for_user(user).roles

user.employments.for_dealer(dealer).roles

关于ruby-on-rails - Rails - 3 列关联表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11848399/

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