Ruby on rails 当我尝试在另一个mongoid下使用“属于”添加新文档时,使用Undefined方法
我正在尝试创建两个文档,一个用于帖子,一个用于回复,并使用“引用1-N”关系将它们连接起来 从中我了解到,您所要做的就是在这两个类中添加Ruby on rails 当我尝试在另一个mongoid下使用“属于”添加新文档时,使用Undefined方法,ruby-on-rails,mongoid,Ruby On Rails,Mongoid,我正在尝试创建两个文档,一个用于帖子,一个用于回复,并使用“引用1-N”关系将它们连接起来 从中我了解到,您所要做的就是在这两个类中添加has\u many和belling\u to,mongoid将允许我添加指向父类的子文档 所以我想做的就是 def create_reply post = Post.find(params[:post_id]) post.reply.new(params[:post]) end 创建指向父级的新回复文档 如果可能,可以使用一个数组来保存子对象的ID
has\u many
和belling\u to
,mongoid将允许我添加指向父类的子文档
所以我想做的就是
def create_reply
post = Post.find(params[:post_id])
post.reply.new(params[:post])
end
class Post
include Mongoid::Document
has_many :replies
field :text,:type => String
end
class Reply
include Mongoid::Document
belongs_to :post
field :name, :type => String
field :text, :type => String
end
控制器
def create_reply
post = Post.find(params[:post_id])
post.reply.new(params[:post])
end
reply
对于Post
类是未定义的,因为Post
有许多回复,而不是reply
试着在你的create\u reply
方法中编写post.repress.new(params[:post])
。谢谢,你是个救命恩人。有没有办法在paren中保留一个子元素数组以便于访问?使用该语法创建子元素并保存父元素后,将保存两者之间的关联。因此,您可以使用post.replements
访问帖子上的所有回复,其中post
是post
类的一个实例。