尝试创建类似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
BUT 似乎没有保存parent_comment。尝试使用控制台,您可以设置父级和子级,但它不会保存到数据库。
更新 修复了未定义的第一个问题,新错误
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或创建另一个模型。
答案 0 :(得分: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
答案 1 :(得分:0)
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