MongoDB mongodb 一对多关系的建立

menghuanwd · 2012年12月20日 · 最后由 QueXuQ 回复于 2012年12月20日 · 4186 次阅读

在 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
需要 登录 后方可回复, 如果你还没有账号请 注册新账号