reddit のようなものを作成しようとすると、すべてのコメントに投稿があり、すべてのコメントに子コメントが含まれる可能性があります。ember.js の json API を作成します。
私の作成方法:
def create
@comment = Comment.new
@comment.text = params[:comment][:text]
@comment.user = current_user
@comment.created = Time.now
if params[:comment][:parent_comment]
@parent = Comment.find(params[:comment][:parent_comment])
@comment.parent_comment = @parent
end
@comment.post = Post.find(params[:comment][:post_id])
@comment.save
end
モデル:
class Comment
include Mongoid::Document
field :text, type: String
field :created, type: Time, default: Time.now
belongs_to :user
embedded_in :post
recursively_embeds_many
end
エラー:
Started POST "/api/v1/comments" for 127.0.0.1 at 2013-07-16 16:34:49 +0300
Processing by Api::V1::CommentsController#create as JSON
Parameters: {"comment"=>{"text"=>"123", "created"=>nil, "user_id"=>"51e51f301c3167fb35000001", "post_id"=>"51e549961c3167ee53000002", "parent_comment_id"=>nil}}
MOPED: 127.0.0.1:27017 QUERY database=ember_js_development collection=users selector={"$query"=>{"_id"=>"51e51f301c3167fb35000001"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.0000ms)
MOPED: 127.0.0.1:27017 QUERY database=ember_js_development collection=posts selector={"_id"=>"51e549961c3167ee53000002"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.0000ms)
Completed 500 Internal Server Error in 10ms
NoMethodError (undefined method `first' for #<Comment:0x0000000ba45c08>):
app/controllers/api/v1/comments_controller.rb:24:in `create'
Rendered C:/Programming/RailsInstaller/Ruby2.0.0-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
Rendered C:/Programming/RailsInstaller/Ruby2.0.0-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered C:/Programming/RailsInstaller/Ruby2.0.0-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered C:/Programming/RailsInstaller/Ruby2.0.0-x64/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (44.0ms)
Github: https://github.com/mongoid/mongoid/issues/3162
更新 この方法は機能します:
def create
@comment = Comment.new
@comment.text = params[:comment][:text]
@comment.user = current_user
@comment.created = Time.now
if params[:comment][:parent_comment]
@parent = Comment.find(params[:comment][:parent_comment])
@comment.parent_comment = @parent
end
@comment.post = Post.find(params[:comment][:post_id])
@comment.save
render json: @comment
end
しかし、 parent_comment が保存されていないようです。コンソールで遊んでみましたが、親と子を設定できますが、データベースに保存されません。
UPDATE 未定義の最初の問題、新しいエラーを修正しました
Mongoid::Errors::InvalidPath:
Problem:
Having a root path assigned for Comment is invalid.
Summary:
Mongoid has two different path objects for determining the location of a document in the database, Root and Embedded. This error is raised when an embedded document somehow gets a root path assigned.
投稿やサブコメントにコメントを埋め込むことができないようです。mongoid_acts_as_tree を考えたり、別のモデルを作成したりしています。