rake db: migrate cause StandardError - неинициализированная константа CreateObjects :: Object

У меня есть переход 20150930051523_create_objects.rb:

class CreateObjects < ActiveRecord::Migration
def change
  create_table :objects do |t|
    t.text :name
    t.timestamps null: false
  end

  Object.create :name => "A"
  Object.create :name => "B"
  Object.create :name => "C"

  end
end
$ rake:db migrate --trace cause output:
** Invoke db:migrate (first_time)
** Invoke db:environment (first_time)
** Execute db:environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
== 20150930051523 CreateObjects: migrating ====================================
-- create_table(:objects)
   -> 0.0010s
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

uninitialized constant CreateObjects::Object/db/migrate/20150930051523_create_objects.rb:8:in 'change'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:605:in 'exec_migration'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:589:in 'block (2 levels) in migrate'
/usr/lib/ruby/2.1.0/benchmark.rb:279:in 'measure'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:588:in 'block in migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in 'with_connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:587:in 'migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:765:in 'migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:995:in 'block in execute_migration_in_transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:1041:in 'block in ddl_transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in 'block in transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/transaction.rb:184:in 'within_new_transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in 'transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/transactions.rb:220:in 'transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:1041:in 'ddl_transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:994:in 'execute_migration_in_transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:956:in 'block in migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:952:in 'each'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:952:in 'migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:820:in 'up'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:798:in 'migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:137:in 'migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/railties/databases.rake:44:in 'block (2 levels) in '
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in 'call'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in 'block in execute'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in 'each'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in 'execute'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:179:in 'block in invoke_with_call_chain'
/usr/lib/ruby/2.1.0/monitor.rb:211:in 'mon_synchronize'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:172:in 'invoke_with_call_chain'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:165:in 'invoke'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:150:in 'invoke_task'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in 'block (2 levels) in top_level'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in 'each'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in 'block in top_level'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:115:in 'run_with_threads'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:100:in 'top_level'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:78:in 'block in run'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:176:in 'standard_exception_handling'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:75:in 'run'
/var/lib/gems/2.1.0/gems/rake-10.4.2/bin/rake:33:in ''
/usr/local/bin/rake:23:in 'load'
/usr/local/bin/rake:23:in ''
NameError: uninitialized constant CreateObjects::Object/db/migrate/20150930051523_create_barbers.rb:8:in 'change'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:605:in 'exec_migration'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:589:in 'block (2 levels) in migrate'
/usr/lib/ruby/2.1.0/benchmark.rb:279:in 'measure'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:588:in 'block in migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in 'with_connection'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:587:in 'migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:765:in 'migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:995:in 'block in execute_migration_in_transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:1041:in 'block in ddl_transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in 'block in transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/transaction.rb:184:in 'within_new_transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in 'transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/transactions.rb:220:in 'transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:1041:in 'ddl_transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:994:in 'execute_migration_in_transaction'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:956:in 'block in migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:952:in 'each'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:952:in 'migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:820:in 'up'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/migration.rb:798:in 'migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/tasks/database_tasks.rb:137:in 'migrate'
/var/lib/gems/2.1.0/gems/activerecord-4.2.4/lib/active_record/railties/databases.rake:44:in 'block (2 levels) in '
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in 'call'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:240:in 'block in execute'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in 'each'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:235:in 'execute'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:179:in 'block in invoke_with_call_chain'
/usr/lib/ruby/2.1.0/monitor.rb:211:in 'mon_synchronize'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:172:in 'invoke_with_call_chain'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/task.rb:165:in 'invoke'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:150:in 'invoke_task'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in 'block (2 levels) in top_level'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in 'each'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:106:in 'block in top_level'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:115:in 'run_with_threads'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:100:in 'top_level'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:78:in 'block in run'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:176:in 'standard_exception_handling'
/var/lib/gems/2.1.0/gems/rake-10.4.2/lib/rake/application.rb:75:in 'run'
/var/lib/gems/2.1.0/gems/rake-10.4.2/bin/rake:33:in ''
/usr/local/bin/rake:23:in 'load'
/usr/local/bin/rake:23:in ''
Tasks: TOP => db:migrate

Ошибка, возникающая на этапе Object.create..., что не так?

+1
источник поделиться
2 ответа

Проблема была решена путем установки pg gem, которые вытаскивают пучок других драгоценных камней и упаковок (особенно postgresql-server-dev-all). После его установки команда rake db: migrate работает нормально и полностью.

0
источник

Обновление. В комментарии было указано, что фактический код не содержит "Объект", поэтому этот ответ основан на неправильном примере.

Object - это плохое имя, которое следует использовать для (не облагороженного) класса модели, поскольку он уже определен Ruby.

Object вы вызываете create on, вероятно, не является классом Object вы определили в ваших источниках модели.

Я предполагаю, что вы управляете рельсами. Удалите строки Object.create из вашей миграции. Запустите его, он должен работать нормально. Затем позвоните в rails console, где вы закончите сеанс в реальном времени. Здесь вы можете поиграть с кодом, например, вызвать Object.create, Object.where{id: 1} и т.д. Я предполагаю, что вы получите те же ошибки.

Итак, в качестве решения переименуйте свой Object в нечто другое, например Class и посмотрите, работает ли он.

Хорошо, это была шутка, конечно, Class тоже очень плохое имя для модельного класса :). Попытайтесь найти что-то более значимое.

0
источник

Посмотрите другие вопросы по меткам или Задайте вопрос