Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.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/ruby-on-rails/55.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
Sql 使用多对多rails关联避免重复行_Sql_Ruby On Rails_Ruby_Activerecord_Rails Activerecord - Fatal编程技术网

Sql 使用多对多rails关联避免重复行

Sql 使用多对多rails关联避免重复行,sql,ruby-on-rails,ruby,activerecord,rails-activerecord,Sql,Ruby On Rails,Ruby,Activerecord,Rails Activerecord,我见过很多多对多的关联,但似乎普遍的趋势是,他们最终会使用has_许多:通过关系 假设你有以下几点: class User < ActiveRecord::Base has_many :user_relationships has_many :relations, :through => :user_relationships end class UserRelationship < ActiveRecord::Base belongs_to :use

我见过很多多对多的关联,但似乎普遍的趋势是,他们最终会使用has_许多:通过关系

假设你有以下几点:

class User < ActiveRecord::Base
    has_many :user_relationships
    has_many :relations, :through => :user_relationships
end

class UserRelationship < ActiveRecord::Base
    belongs_to :user
    belongs_to :related_user, class_name: "User"
end
导致联接表中的行如下所示:

user_id | related_user_id
1       | 2
2       | 1
因此,当设置上述关系时,您可以看到可以完成以下操作

user1.relations.include? user2 = true
user2.relations.include? user1 = true
我的问题是:有没有一种方法可以在Rails中实现上述功能,或者至少在速度上类似于上述功能,而不必为每个双向关系创建两行,并保持从两端高效查看关系的能力,将创建此关系的空间复杂性减少一半

如果这是一个无趣的问题,我深表歉意,但我对Rails还不熟悉,刚刚开始掌握一些窍门。很容易找到如何设置它们,但我发现更难找到如何以有效的方式实际实现它们。我将跳过很多细节,但如果有帮助的话,我很乐意分享更多。这可能是令人发指的,所以我很好奇别人的想法

本质上,我有一个
关系
模型,类似于:

module ActsAsRelatingTo
  class Relationship < ActiveRecord::Base

    validates :owner_id,                  presence: true
    validates :owner_type,                presence: true
    validates :in_relation_to_id,         presence: true
    validates :in_relation_to_type,       presence: true

    belongs_to  :owner,                   polymorphic: true
    belongs_to  :in_relation_to,          polymorphic: true

    acts_as_taggable
    acts_as_taggable_on :roles

  end
end
class Person < ActiveRecord::Base
  # Here, I am calling the method that I defined above, passing in
  # :people, :organizations, and :programs. This is exactly the 
  # sort of thing you do all the time when you say something like
  # 'has_one :foo', or 'belongs_to :bar'. 
  acts_as_relating_to :people, :organizations, :programs

  # Here, I am calling a method I have that builds on acts_as_relating_to,
  # but which I did not show, that creates administrative methods on 
  # the person so that I can say stuff like 'person.administrate organization'.
  # Or, 'organization.administrators'. 
  acts_as_administering :organizations, :programs

  ... 

end
然后,我可以做一些类似的事情:

module ActsAsRelatingTo
  class Relationship < ActiveRecord::Base

    validates :owner_id,                  presence: true
    validates :owner_type,                presence: true
    validates :in_relation_to_id,         presence: true
    validates :in_relation_to_type,       presence: true

    belongs_to  :owner,                   polymorphic: true
    belongs_to  :in_relation_to,          polymorphic: true

    acts_as_taggable
    acts_as_taggable_on :roles

  end
end
class Person < ActiveRecord::Base
  # Here, I am calling the method that I defined above, passing in
  # :people, :organizations, and :programs. This is exactly the 
  # sort of thing you do all the time when you say something like
  # 'has_one :foo', or 'belongs_to :bar'. 
  acts_as_relating_to :people, :organizations, :programs

  # Here, I am calling a method I have that builds on acts_as_relating_to,
  # but which I did not show, that creates administrative methods on 
  # the person so that I can say stuff like 'person.administrate organization'.
  # Or, 'organization.administrators'. 
  acts_as_administering :organizations, :programs

  ... 

end
class-Person
因此,如果我有
person\u 1
person\u 2
,我有一个关系记录,其中
所有者
person\u 1
,与
相关的
person\u 2
,那么我可以说
person\u 1。person\u与
相关并返回
person\u 2
。或者,我可以说,
person\u 2.与我相关的人
,然后返回
person\u 1

我还学习了一个
扮演管理者
模块,它建立在
扮演与
模块相关的角色,让我可以做一些事情,比如
人1.管理的组织
人1.管理的程序

我可能讲得太久了。无论如何,如果有意思的话,我可以说得更多


干杯

非常整洁,但有点让我不知所措,不熟悉ruby的反射api和class_eval的功能
class Person < ActiveRecord::Base
  # Here, I am calling the method that I defined above, passing in
  # :people, :organizations, and :programs. This is exactly the 
  # sort of thing you do all the time when you say something like
  # 'has_one :foo', or 'belongs_to :bar'. 
  acts_as_relating_to :people, :organizations, :programs

  # Here, I am calling a method I have that builds on acts_as_relating_to,
  # but which I did not show, that creates administrative methods on 
  # the person so that I can say stuff like 'person.administrate organization'.
  # Or, 'organization.administrators'. 
  acts_as_administering :organizations, :programs

  ... 

end