不确定轨道的祖先宝石是如何工作的

时间:2014-06-17 16:23:11

标签: ruby-on-rails ruby ancestry

我正在尝试使用我通过观看旧ancestry gem找到的Ryan Bates Ancestry video,因此我设置了我的东西:

迁移:

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,但这也无济于事。有谁知道我做错了什么?或有任何想法?

1 个答案:

答案 0 :(得分:0)

post.comments.new(:ancenstry => params[:parent_id])

哈希的密钥拼写错误。