Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.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模型关联?_Ruby On Rails_Ruby_Model_Associations - Fatal编程技术网

Ruby on rails 在这种情况下,我应该使用哪种类型的Rails模型关联?

Ruby on rails 在这种情况下,我应该使用哪种类型的Rails模型关联?,ruby-on-rails,ruby,model,associations,Ruby On Rails,Ruby,Model,Associations,我的Rails应用程序中有两个模型/表:讨论和评论。每个讨论都有许多评论,每个评论都属于一个讨论。我的讨论表还包括一个first\u comment\u id列和last\u comment\u id列,以方便快捷。我希望能够为最后一条评论模型调用讨论。最后一条评论,但以下内容(在我的讨论模型中)无法实现这一点: has_one :first_comment, :class_name => "Comment" has_one :last_comment, :class_name =>

我的Rails应用程序中有两个模型/表:讨论和评论。每个讨论
都有许多
评论,每个评论
都属于一个讨论。我的讨论表还包括一个
first\u comment\u id
列和
last\u comment\u id
列,以方便快捷。我希望能够为最后一条评论模型调用
讨论。最后一条评论
,但以下内容(在我的讨论模型中)无法实现这一点:

has_one :first_comment, :class_name => "Comment"
has_one :last_comment, :class_name => "Comment"
当我调用
discussion.last_comment
时,将运行以下SQL:

SELECT * FROM `comments` WHERE (`comments`.discussion_id = 1) LIMIT 1
它使用
discussions.id
列加入
评论。当我希望它加入
讨论时,它使用
discussion.id
列加入
comments.id


我在这里使用了错误的关联类型吗?谢谢你的帮助

尝试一个
属于
,而不是一个
有一个

命名可能有些奇怪,但两者之间的区别实际上只是关于哪个表包含外键列