Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.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 On Rails 4_Associations - Fatal编程技术网

Ruby on rails rails有很多,也有一个

Ruby on rails rails有很多,也有一个,ruby-on-rails,ruby-on-rails-4,associations,Ruby On Rails,Ruby On Rails 4,Associations,我有两种型号: 用户: class User < ActiveRecord::Base has_many :comment class用户

我有两种型号:

用户:

class User < ActiveRecord::Base
  has_many :comment
class用户
评论:

class Comment < ActiveRecord::Base
  belongs_to :movie
  has_one :user
end
class注释
我现在想要的是: 每个评论都与一个用户完全相关

但是每个用户都可以有mandy评论

但当我想将其存储到db时,我得到了以下错误:

我的错误在哪里?

关于您的代码:

class Comment < ActiveRecord::Base
  belongs_to :movie
  has_one :user
end
致:

现在,您可以在代码上使用,
@comment.user=@user

class Comment < ActiveRecord::Base
  belongs_to :movie
  has_one :user
end
致:


您现在可以使用,
@comment.user=@user

您定义关系的方式可能错误,可能您必须使用属于其中一个模型,而哪一个?在注释数据库中,我在注释模型中设置了user\u id列“放置属于”以使其工作吗?对你来说?为什么这个问题被否决了?你定义关系的方式可能是错误的,可能是你必须使用属于其中一个模型,而哪一个?在评论数据库中,我在评论模型中有用户id列,放置属于它工作吗?为你?为什么这个问题被否决了?
belongs_to :user