Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在Rails中,三方关系可以这样建模吗?_Ruby On Rails - Fatal编程技术网

Ruby on rails 在Rails中,三方关系可以这样建模吗?

Ruby on rails 在Rails中,三方关系可以这样建模吗?,ruby-on-rails,Ruby On Rails,一个用户可以有多个角色,但每个品牌只能有一个角色 Class User < AR::Base has_and_belongs_to_many :roles, :join_table => "user_brand_roles" has_and_belongs_to_many :brands, :join_table => "user_brand_roles" end 这是一个新的应用程序,我正试图从过去的错误中吸取教训,坚持Rails的方式。我在这里是否做出了错

一个用户可以有多个角色,但每个品牌只能有一个角色

Class User < AR::Base
    has_and_belongs_to_many :roles, :join_table => "user_brand_roles"
    has_and_belongs_to_many :brands, :join_table => "user_brand_roles"
end

这是一个新的应用程序,我正试图从过去的错误中吸取教训,坚持Rails的方式。我在这里是否做出了错误的假设或设计决策?

假设角色,品牌是参考表。您可以有一个包含用户id、角色id、品牌id的列的关联表职责。 然后你可以定义

Class User < AR::Base
 has_many : responsibilities
 has_many :roles, :through => responsibilities
 has_many :brands,:through => responsibilities
end
Class Responsibility < AR::Base
 belongs_to :user
 has_one :role
 has_one :brand
end
Class User < AR::Base
  def has_access?(brand)
     responsibility = responsibilities.where(:brand => brand)
     responsibility and responsibility.role == ADMIN
  end
end
Class用户职责
有很多:品牌,:通过=>责任
结束
类责任
您可以定义的

Class User < AR::Base
 has_many : responsibilities
 has_many :roles, :through => responsibilities
 has_many :brands,:through => responsibilities
end
Class Responsibility < AR::Base
 belongs_to :user
 has_one :role
 has_one :brand
end
Class User < AR::Base
  def has_access?(brand)
     responsibility = responsibilities.where(:brand => brand)
     responsibility and responsibility.role == ADMIN
  end
end
Class用户品牌)
responsibility和responsibility.role==管理员
结束
结束

[不确定职责是否是您的领域中使用的术语,但请使用领域术语,而不是将其称为用户_品牌_角色]

假设角色,品牌是参考表。您可以有一个包含用户id、角色id、品牌id的列的关联表职责。 然后你可以定义

Class User < AR::Base
 has_many : responsibilities
 has_many :roles, :through => responsibilities
 has_many :brands,:through => responsibilities
end
Class Responsibility < AR::Base
 belongs_to :user
 has_one :role
 has_one :brand
end
Class User < AR::Base
  def has_access?(brand)
     responsibility = responsibilities.where(:brand => brand)
     responsibility and responsibility.role == ADMIN
  end
end
Class用户职责
有很多:品牌,:通过=>责任
结束
类责任
您可以定义的

Class User < AR::Base
 has_many : responsibilities
 has_many :roles, :through => responsibilities
 has_many :brands,:through => responsibilities
end
Class Responsibility < AR::Base
 belongs_to :user
 has_one :role
 has_one :brand
end
Class User < AR::Base
  def has_access?(brand)
     responsibility = responsibilities.where(:brand => brand)
     responsibility and responsibility.role == ADMIN
  end
end
Class用户品牌)
responsibility和responsibility.role==管理员
结束
结束

[不确定责任是否是您的领域中使用的术语,但请使用领域术语,而不是将其称为用户_品牌_角色]

这是一个概念性的问题。如果BrandRole是应用程序的一个实体,那么您的方法应该有效。如果BrandRole本身不是应用程序中的实体,那么您可以创建UserBrandRole模型:

class User < AR::Base
  has_many :user_brand_roles
end

class Brand < AR::Base
  has_many :user_brand_roles
end

class Role < AR::Base
  has_many :user_brand_roles
end

class UserBrandRole < AR::Base
  belongs_to :user
  belongs_to :brand
  belongs_to :role
  validates_uniqueness_of :role_id, :scope => [:user_id, :brand_id]
end
class用户[:用户id,:品牌id]
结束

这是一个概念性的东西。如果BrandRole是应用程序的一个实体,那么您的方法应该有效。如果BrandRole本身不是应用程序中的实体,那么您可以创建UserBrandRole模型:

class User < AR::Base
  has_many :user_brand_roles
end

class Brand < AR::Base
  has_many :user_brand_roles
end

class Role < AR::Base
  has_many :user_brand_roles
end

class UserBrandRole < AR::Base
  belongs_to :user
  belongs_to :brand
  belongs_to :role
  validates_uniqueness_of :role_id, :scope => [:user_id, :brand_id]
end
class用户[:用户id,:品牌id]
结束

首先,请描述您试图实现的目标,因为听起来您不了解如何创建关联。那么帮助您就容易多了:)谢谢Sam。我理解Rails中的关系,但我认为我的困惑程度比这要早一步。我不知道我问的问题是否正确。我正在尝试设置一种情况,用户可以使用特定的权限集访问站点的受保护区域。一家公司有几个品牌,每个品牌都包含多个产品。有些员工只需要查看品牌内产品的一般信息,有些员工需要管理员级别的访问权限。首先,请描述您试图实现的目标,因为听起来您不了解如何创建关联。那么帮助您就容易多了:)谢谢Sam。我理解Rails中的关系,但我认为我的困惑程度比这要早一步。我不知道我问的问题是否正确。我正在尝试设置一种情况,用户可以使用特定的权限集访问站点的受保护区域。一家公司有几个品牌,每个品牌都包含多个产品。有些员工只需要查看品牌内产品的一般信息,有些员工需要管理员级别的访问权限。