我正在尝试使用通过观看旧的Ryan Bates Ancestry 视频找到的祖先宝石,所以我像这样设置我的东西:
移民:
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :comment
t.string :author
t.integer :post_id
t.string :ancestry
t.index :ancestry
t.timestamps
end
end
end
模型:
class Comment < ActiveRecord::Base
belongs_to :post
has_ancestry
validates :comment, presence: true, length: {minimum: 15}
validates :author, presence: true
end
** 控制器新动作:**
def new
post = Post.find_by(id: params[:post_id])
post.comments.new(:ancenstry => params[:parent_id])
end
所以我认为我已经正确设置了所有内容。但是当我运行以下测试时:
it "should create a nested comment for a post" do
posted_comment = FactoryGirl.create(:comment, post_id: @post.id)
post :create, :post_id => @post.id, comment: {author: @comment.author, comment: @comment.comment, parent_id: @comment.id}
json = JSON.parse(response.body).to_json
binding.pry
parse_json(json, 'comment').to_json.should have_json_path('id')
end
并在绑定撬后检查 json:
{
"comment":{
"id":9,
"post_id":6,
"author":"Adam",
"comment":"Some Really long Valid Comment length of Something.",
"ancestry":null
}
}
祖先部分为空。我什至尝试过更改parent_id
为,ancestry
但这也无济于事。有谁知道我做错了什么?或有什么想法?