Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.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/5/ruby/21.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
Ruby on rails 在Rails 3模型中创建命名关系_Ruby On Rails_Ruby_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby on rails 在Rails 3模型中创建命名关系

Ruby on rails 在Rails 3模型中创建命名关系,ruby-on-rails,ruby,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby,Ruby On Rails 3,Activerecord,我正在努力学习Rails,我正在努力理解如何使用ActiveRecord组件声明自我关系 如果我有这样的东西: class Comment < ActiveRecord::Base has_many :comments belongs_to :comments end has_many :rel, :class_name => "Class" class注释

我正在努力学习Rails,我正在努力理解如何使用ActiveRecord组件声明自我关系

如果我有这样的东西:

class Comment < ActiveRecord::Base
    has_many :comments
    belongs_to :comments
end
has_many :rel, :class_name => "Class"
class注释
作为相关评论、评论回复和评论的父级,如果它们具有相同的名称,我应该如何访问它们?我不能只做注释。注释需要有不同的名称


谢谢。

首先,
属于
是一个单数关联,因此它是:

belongs_to :comment
。。。你不会有名字冲突

但对于确实存在冲突的情况,始终可以重命名关系,例如:

has_many :comments
has_many :recent_comments, :class_name => 'Comment', :limit => 10, :order => 'id DESC'

查看更多关联选项示例。

您需要使用单数表示
所属的关联:

    belongs_to :comment

看起来您正在尝试创建
Comment
s的树状结构。您可能想看看gems,如。

传递给has\u many方法的第一个符号是您要指定的名称。Rails使用约定优先于配置原则,因此它将相关类的名称从它取到,但您可以这样指定它:

class Comment < ActiveRecord::Base
    has_many :comments
    belongs_to :comments
end
has_many :rel, :class_name => "Class"
查看文档,但它将属于注释,而不是注释:)