简体   繁体   中英

ActiveRecord::Migration unitialized constant error.

Disclaimer : I don't know ruby.

I've been trying to setup this ruby application. I'm trying to debug why I get an uninitialized constant error:

rake aborted!
StandardError: An error has occurred, all later migrations canceled:

uninitialized constant CreateSyncsTable::Syncs/var/rails/cycs/cycs/db/migrate/20100818122117_create_syncs_table.rb:8:in `up'

Below is the file contents of 20100818122117_create_syncs_table.rb.

class CreateSyncsTable < ActiveRecord::Migration
  def self.up
    create_table :syncs do |t|
      t.timestamps
      t.integer :remedy_query_low
      t.integer :remedy_query_high
    end
    Sync.create :remedy_query_low => 0, :remedy_query_high => 0
  end

  def self.down
    drop_table :syncs
  end
end

To my understanding, line 8 is trying to create a new entry into the syncs table. I don't know exactly why the syntax for doing so is as follows:

 Sync.create :remedy_query_low => 0, :remedy_query_high => 0

Nor do I understand why or where "Sync" comes from. Moreover, I don't know what constant is uninitialized.

Any help to debug this is appreciated. For the record, I am using centOS 7, Maria DB and ActiveRecord version 3.2.18.

  1. I reviewed the API to see if that would help. Since I don't know ruby, I don't find it of much help.

Try adding in a call to ActiveRecord::ModelSchema.reset_column_information as it "resets all the cached information about columns, which will cause them to be reloaded on the next request", allowing you to create Sync entries immediately after creating the table in the migration:

class CreateSyncsTable < ActiveRecord::Migration
  def self.up
    create_table :syncs do |t|
      t.timestamps
      t.integer :remedy_query_low
      t.integer :remedy_query_high
    end
    Sync.reset_column_information
    Sync.create :remedy_query_low => 0, :remedy_query_high => 0
  end

  def self.down
    drop_table :syncs
  end
end

Personally, I would recommend removing the Sync.create :remedy_query_low => 0, :remedy_query_high => 0 statement altogether since I think Rails migrations are best used for schema migrations only, and data migrations are best put in rake tasks (a great reason why can be found here ).

In this case, Sync is referring to a model. You should have a file in app/models/ called sync.rb , which is the model definition for the Sync class. This is a Rails convention for defining models.

You can use models in your migrations (you've been looking at a migration). However, it's generally not recommended to use your app's models in the migration, because you can end up with conflicts.

What's recommended is to define a proxy model in your migration, which takes the place of the app model for the purposes of the migration. You declare the proxy model at the top of your migration file, so that it's local to the migration.

Try this code:

class Sync < ActiveRecord::Base
end

class CreateSyncsTable < ActiveRecord::Migration
  def self.up
    create_table :syncs do |t|
      t.timestamps
      t.integer :remedy_query_low
      t.integer :remedy_query_high
    end

    # Reset ActiveRecord cache of Sync details
    Sync.reset_column_information

    Sync.create :remedy_query_low => 0, :remedy_query_high => 0
  end

  def self.down
    drop_table :syncs
  end
end

The additional call to Sync.reset_column_information tells Rails to reset the cache of information about the model because the table structure has changed. This will prevent issues, such as trying to access the new columns added in the migration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM