2

我目前正在尝试在 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

The tables look like (green tables):

Model diagram

I am trying to seed the database using the following code:

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',因为它通过多个其他关联。”

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

4

1 回答 1

4

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

# 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
于 2012-08-07T14:54:10.157 回答