Sql 使用多对多rails关联避免重复行
我见过很多多对多的关联,但似乎普遍的趋势是,他们最终会使用has_许多:通过关系 假设你有以下几点: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
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