0

尝试创建类似 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 或创建另一个模型。

4

2 回答 2

0

似乎嵌入式不是正确的选择。

解决方案

class Comment
  include Mongoid::Document

  field :text, type: String
  field :created, type: Time, default: Time.now

  belongs_to :user
  belongs_to :post
  belongs_to :parent_comment, class_name: "Comment", inverse_of: :child_comments
  has_many :child_comments, class_name: "Comment", inverse_of: :parent_comment
end
于 2013-07-17T10:06:29.810 回答
0

当我在 1-1 嵌入关系中创建 1-n 嵌入关系时,这发生在我身上。

level4.business_function = BusinessFunction.new
    level4.business_function.function = row[4].to_s
    level4.business_function.category_tree = (row[0].to_s+"/"+row[1].to_s+"/"+row[2].to_s+"/"+row[3].to_s)
    level4.business_function.unit = row[5].to_s
    level4.business_function.eco = row[6].to_s
    level4.business_function.pto = row[7].to_s
    level4.business_function.save!

这是 1-1,但是当我重复 1-n 时,它给出了你得到的错误。所以我创建了这样的文档并且它有效。

var.each_with_index do |v, index|
  level4.business_function.variations.create(variation: v, base_price: base_prices[index])
end 
于 2014-05-19T11:10:37.770 回答