在rails中为has_many自联接定义表名?
我希望设置嵌套的注释,并希望使用自联接来设置它。
class Comment 'Comment' #... end
现在,我将使用什么sql表结构来设置has_many自连接?
我假设这样的事情:
comment_to_comments: parent_id integer child_id integer
如何告诉rails使用此表? 如何告诉rails parent_id是到达父级的外键,child_id是到达子级的外键?
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
我建议你使用插件来实现这个function。比如awesome_nested_set或acts_as_tree。