У меня в грабле есть такая связь
def some_connection
ActiveRecord::Base.establish_connection({ adapter: 'mysql2',
host: 'localhost',
username: Rails.application.secrets.etalon_db_username,
password: Rails.application.secrets.etalon_db_password,
database: 'somedb' })
.connection
end
И мне нужно выполнить эту инструкцию SQL
UPDATE car_generation SET year_end = 0 WHERE id_car_generation IN (292,488,1663,6542)
Запуск этого:
some_connection.exec_update(fix_query, nil, [])
я получаю ошибку:
rake aborted!
ActiveRecord::StatementInvalid: NoMethodError: undefined method `query' for nil:NilClass
Did you mean? to_query: UPDATE car_generation
SET year_end = 0
WHERE id_car_generation IN (292,488,1663,6542...)
.rvm/gems/ruby-2.3.3@gemset/gems/activerecord-4.1.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `block in execute'
.rvm/gems/ruby-2.3.3@gemset/gems/activerecord-4.1.13/lib/active_record/connection_adapters/abstract_adapter.rb:378:in `block in log'
.rvm/gems/ruby-2.3.3@gemset/gems/activesupport-4.1.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
.rvm/gems/ruby-2.3.3@gemset/gems/activerecord-4.1.13/lib/active_record/connection_adapters/abstract_adapter.rb:372:in `log'
.rvm/gems/ruby-2.3.3@gemset/gems/activerecord-4.1.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `execute'
.rvm/gems/ruby-2.3.3@gemset/gems/activerecord-4.1.13/lib/active_record/connection_adapters/mysql2_adapter.rb:228:in `execute'
.rvm/gems/ruby-2.3.3@gemset/gems/activerecord-4.1.13/lib/active_record/connection_adapters/mysql2_adapter.rb:255:in `exec_delete'
lib/tasks/etalon_db_tables_population.rake:15:in `block (3 levels) in <top (required)>'
lib/tasks/etalon_db_tables_population.rake:10:in `each'
lib/tasks/etalon_db_tables_population.rake:10:in `block (2 levels) in <top (required)>'
.rvm/gems/ruby-2.3.3@gemset/gems/rake-12.3.2/exe/rake:27:in `<top (required)>'
.rvm/gems/ruby-2.3.3@gemset/bin/ruby_executable_hooks:24:in `eval'
.rvm/gems/ruby-2.3.3@gemset/bin/ruby_executable_hooks:24:in `<main>'
Та же ошибка появляется при использовании execute вместо exec_query .
Но все в порядке, если я выполню этот код во время миграции на Rails.