1

I am trying to install the Ancestry gem but I am having problems with rake db:migrate.

I am following the instructions on the Ancestry github page. After I have done rails g migration add_ancestry_to_message ancestry:string I am editing the migration file (following railcast #262) to be:

class AddAncestryToMessage < ActiveRecord::Migration
  def self.up
    add_column :messages, :ancestry, :string
    add_index :messages, :ancestry
  end

  def self.down
    remove_index :messages, :ancestry
    remove_column :messages, :ancestry
  end
end

When I then run rake db:migrate I am getting the following error:

==  AddAncestryToMessage: migrating ===========================================
-- add_column(:messages, :ancestry, :string)
rake aborted!
An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: no such table: Shipmgr: ALTER TABLE "Message" ADD "ancestry" varchar(255)

Tasks: TOP => db:migrate

I have tried this on a newly created rails app and on an existing rails app but I am still unable to get this to work. Does anyone have any advice on this issue?

Tom Kadwill
  • 1,448
  • 3
  • 15
  • 21

1 Answers1

0

You should try changing the migration class name to the pluralized (table) form 'Messages':

class AddAncestryToMessages < ActiveRecord::Migration

or, more accurately, change the migration generator command to:

rails g migration add_ancestry_to_messages ancestry:string
PinnyM
  • 35,165
  • 3
  • 73
  • 81
  • Thanks for your suggestion. I have tried this and it is still throwing the same error message. I have set it up in the way that you describe, I also have messages.rb in the models containing `class Messages < ActiveRecord::Base has_ancestry end ` – Tom Kadwill Jan 12 '12 at 10:40
  • I think the problem might be how you are using plurals and singular names throughout your code. Here are the general rules: Model class name and model file name should use the singular (class Message and message.rb). The migrations should refer to the table name which is always plural. There are ways to change this default behavior, but first try conforming with the defaults and see if that works – PinnyM Jan 12 '12 at 14:45
  • Also, can you confirm that you have a previous migration in place to create the :messages table? – PinnyM Jan 12 '12 at 17:05
  • With regards to using plural and singular names, I have tried a couple of different variations. I have tried following the [ancestry railscast](http://railscasts.com/episodes/262-trees-with-ancestry?view=asciicast) exactly using a newly generated rails app. Before following the railscast I did nothing except a bundle install and a rake db:migrate. – Tom Kadwill Jan 12 '12 at 19:05
  • thank you for your patience, I had not created the :messages table correctly. Thanks again for your help. – Tom Kadwill Jan 13 '12 at 11:37