Search code examples
ruby-on-railsrubyancestry

Unsure how the ancestry gem for rails works


I am trying to use the ancestry gem which I found by watching an old Ryan Bates Ancestry video and so I set up my my stuff like so:

Migration:

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

Model:

class Comment < ActiveRecord::Base
  belongs_to :post
  has_ancestry

  validates :comment, presence: true, length: {minimum: 15}
  validates :author, presence: true
end

** Controller new Action: **

  def new
    post = Post.find_by(id: params[:post_id])
    post.comments.new(:ancenstry => params[:parent_id])
  end

So I think I have set up everything correctly. But when I run the following test:

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

And inspect the json after the binding pry:

{
   "comment":{
      "id":9,
      "post_id":6,
      "author":"Adam",
      "comment":"Some Really long Valid Comment length of Something.",
      "ancestry":null
   }
}

The ancestry section is null. I have even tried tried changing parent_id to ancestry but that doesn't help either. Does any know what I am doing wrong? or have any ideas?


Solution

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

    The key of your hash is misspelled.