Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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 3 独特关联:通过_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 3 独特关联:通过

Ruby on rails 3 独特关联:通过,ruby-on-rails-3,Ruby On Rails 3,我有一个多对多:通过一组类之间的关系,如下所示: class Company has_many :shares has_many :users, :through => :shares, :uniq => true end class User has_many :shares has_many :companys, :through => :shares, uniq => true end class Share belongs_to :comp

我有一个多对多:通过一组类之间的关系,如下所示:

class Company
  has_many :shares
  has_many :users, :through => :shares, :uniq => true
end

class User
  has_many :shares
  has_many :companys, :through => :shares, uniq => true
end

class Share
  belongs_to :company
  belongs_to :user
end
我希望确保一种独特的关系,这样一个用户在任何一家公司只能拥有一份股份,这就是我使用“uniq”参数试图实现的

起初我认为这是可行的,但“uniq”的行为似乎是根据记录的选择进行过滤,而不是预插入,因此我仍然在数据库中获得重复记录,如果我想开始直接处理:股票协会,这将成为一个问题,as calling user.shares将返回重复记录(如果存在)


有谁能帮助我们找到一种真正的uniq关系?因此,如果我尝试添加用户和公司之间的第二个关系,它将拒绝它,只保留原始关系?

您是否尝试将其添加到您的
共享类中

validates_uniqueness_of :user, scope: :company
另外,在你的
用户
课程中,我认为应该是:

has_many :companies, through: :shares
我希望这有帮助