MongoDB mongodb 一对多关系的建立

menghuanwd · December 20, 2012 · Last by QueXuQ replied at December 20, 2012 · 4185 hits

在 model 中: class Comment field :text, type: String embedded_in :user end

class User field :name, type: String field :age, type: Integer embeds_many :comments end

controller 中:def addComment @user = User.find(params[:user_id]) #错误的 @comment = @user.comments.new(params[:comment]) @comment.save redirect_to user_path('50d2aa5bc720b27792000002') end

应该怎么在 controller 中写如保存方法呢?如果嵌套的话那还需不需要这两个 modelne ??哪里有正确的教程啊...

我没有用过 mongodb,但是那个关联的创建是不是应该是这样:

@user = User.find(params[:user_id]) 
@comment = @user.comments.build(params[:comment])  #用build,而不是new
@comment.save
redirect_to user_path('50d2aa5bc720b27792000002')
end
You need to Sign in before reply, if you don't have an account, please Sign up first.