定义has_many self联接的表名吗? [英] Defining table name for has_many self joins in rails?
本文介绍了定义has_many self联接的表名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望设置嵌套注释,并希望使用自连接进行设置。
I wish to set up nested comments, and want to use self-join to set this up.
class Comment < ActiveRecord::Base
has_many :children, :class_name => 'Comment'
#...
end
现在,我将使用什么sql表结构来设置has_many自联接?
Now, what sql table structure would I use to set up the has_many self-join?
我假设是这样的:
comment_to_comments:
parent_id integer
child_id integer
我如何告诉rails使用此表?如何告诉rails parent_id是到达父级的外键,child_id是到达子级的外键?
How do I tell rails to use this table? How do I tell rails that parent_id is the foreign key to reach the parent and the child_id is the foreign key to reach the child?
推荐答案
create_table :comments do |t|
t.integer :parent_id
end
class Comment < ActiveRecord::Base
has_many :children, :class_name => "Comment", :foreign_key => :parent_id
belongs_to :parent, :class_name => "Comment"
end
我建议您使用插件来实现此功能.like awesome_nested_set或acts_as_tree。
I suggest you use plugin to implement this feature.like awesome_nested_set or acts_as_tree.
这篇关于定义has_many self联接的表名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文