Ruby on rails 自加入协会有多个,属于(一个)
我想建立一个有两种关系的自连接协会 有许多并且属于(一) 我找到了两种方法,但我不确定哪一种更准确,例如,在这个答案中,员工和经理之间存在关系,经理有很多员工,而员工属于经理 解决方案是添加一个名为经理id的字段,因此如果用户是经理,那么经理id将为Ruby on rails 自加入协会有多个,属于(一个),ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我想建立一个有两种关系的自连接协会 有许多并且属于(一) 我找到了两种方法,但我不确定哪一种更准确,例如,在这个答案中,员工和经理之间存在关系,经理有很多员工,而员工属于经理 解决方案是添加一个名为经理id的字段,因此如果用户是经理,那么经理id将为null,如果是员工,那么经理id将是经理的id 这些关联如下所示: has_many :employees, class_name: "User", foreign_key: :manager_id belongs_to :manager, cla
null
,如果是员工,那么经理id将是经理的id
这些关联如下所示:
has_many :employees, class_name: "User", foreign_key: :manager_id
belongs_to :manager, class_name: "User", foreign_key: :manager_id
create_table "posts", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "related_posts", :force => true do |t|
t.integer "post_id"
t.integer "related_post_id"
t.datetime "created_at"
t.datetime "updated_at"
end
class Post < ActiveRecord::Base
has_many :related_posts_association, :class_name => "RelatedPost"
has_many :related_posts, :through => :related_posts_association, :source => :related_post
has_many :inverse_related_posts_association, :class_name => "RelatedPost", :foreign_key => "related_post_id"
has_many :inverse_related_posts, :through => :inverse_related_posts_association, :source => :post
end
class RelatedPost < ActiveRecord::Base
belongs_to :post
belongs_to :related_post, :class_name => "Post"
end
我在这里找到的第二个解决方案
具有以下关系:帖子有很多相关帖子,相关帖子属于帖子
这里的解决方案是创建两个单独的表,一个用于POST,另一个用于相关的_POST,如下所示:
has_many :employees, class_name: "User", foreign_key: :manager_id
belongs_to :manager, class_name: "User", foreign_key: :manager_id
create_table "posts", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "related_posts", :force => true do |t|
t.integer "post_id"
t.integer "related_post_id"
t.datetime "created_at"
t.datetime "updated_at"
end
class Post < ActiveRecord::Base
has_many :related_posts_association, :class_name => "RelatedPost"
has_many :related_posts, :through => :related_posts_association, :source => :related_post
has_many :inverse_related_posts_association, :class_name => "RelatedPost", :foreign_key => "related_post_id"
has_many :inverse_related_posts, :through => :inverse_related_posts_association, :source => :post
end
class RelatedPost < ActiveRecord::Base
belongs_to :post
belongs_to :related_post, :class_name => "Post"
end
这些协会看起来是这样的:
has_many :employees, class_name: "User", foreign_key: :manager_id
belongs_to :manager, class_name: "User", foreign_key: :manager_id
create_table "posts", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "related_posts", :force => true do |t|
t.integer "post_id"
t.integer "related_post_id"
t.datetime "created_at"
t.datetime "updated_at"
end
class Post < ActiveRecord::Base
has_many :related_posts_association, :class_name => "RelatedPost"
has_many :related_posts, :through => :related_posts_association, :source => :related_post
has_many :inverse_related_posts_association, :class_name => "RelatedPost", :foreign_key => "related_post_id"
has_many :inverse_related_posts, :through => :inverse_related_posts_association, :source => :post
end
class RelatedPost < ActiveRecord::Base
belongs_to :post
belongs_to :related_post, :class_name => "Post"
end
class Post“RelatedPost”
有很多:相关的帖子,:通过=>:related\u posts\u association,:source=>:related\u post
有很多:反向相关帖子关联,:class\u name=>“RelatedPost”,:foreign\u key=>“RelatedPost\id”
有很多:反向相关的帖子,:通过=>:反向相关的帖子\u关联,:源=>:帖子
结束
类RelatedPost“职位”
结束
现在我不确定该怎么办,第一个解决方案看起来很简单,想法是添加一个名为manager_id的附加字段(如果我们处理posts,则可以是post_id),该字段对于所有managers人员都为空,我想象在该字段上有许多空值的记录,这似乎不正确
第二个解决方案看起来不错,但我觉得不对的是关系有很多:反向相关帖子关联,似乎相关帖子也有其他相关帖子/或者相关帖子属于很多帖子(我不确定)
在我的例子中,我希望类似于post的东西有很多相关的帖子,一个相关的帖子属于(一个)帖子我会选择第一个选项,除非你有非常令人信服的理由采取第二种方法。这是简单而明显的。你可以在以后重构你的代码
列中有空值没有什么特别糟糕的,只要该列与所有记录相关。在您的情况下,manager_id中的NULL表示“此人没有经理”。
空值与此相关吗?我的回答是肯定的
NULL表示该值未知或没有值。Rails语句@person.manager.present?
对于定义了manager的人将正确返回true
,对于没有manager的人将正确返回false
。当查看任何特定人员的数据库记录时,manager_id字段中的NULL将传达相同的含义。在数据库建模中,第一个选项是显而易见的正确方法,但想想该字段中数百万个具有NULL值的记录,我听说有很多NULL是一件坏事!你所说的与所有记录相关是什么意思?我在回答中扩展了相关部分,并将在评论中回答你的其余问题。从体系结构的角度来看,如果你的表中有许多可为空的列,并且记录会被稀疏地填充(每个记录都应该有许多空),这可能是不良数据库设计的标志(首先要考虑的是一些规范化)。但这听起来与你的情况不同。“想想那个字段中有数百万条空值记录”——你的应用程序是否有理由存储数百万经理的信息?据我所知,没有这样规模的公司。在这种情况下,经理与员工的预期比例是多少?旁注:大多数RDBMS系统在中等硬件上可以处理数百万条记录。经理员工只是解释这个问题的一个例子,我的案例更类似于与帖子相关的帖子,是的,如果你有某种社交网络,比如人们发布东西的话,你可以看到数百万条帖子。。。我担心的只是该字段中有数百万行的空值