前提・実現したいこと
Rubyで記述したプログラム上で、ActiveRecordを通してPostgreSQLを操作したい。
teachersテーブルは以下のカラムを持っています。
- id
- name
- subject
Ruby
1require 'active_record' 2 3ActiveRecord::Base.establish_connection( 4 adapter: "postgresql", 5 host: "", 6 username: "kampachi", 7 password: "", 8 database: "testdb01" 9) 10 11class Teacher < ActiveRecord::Base 12end 13 14teacher = Teacher.find_by(name: "Kampachi") 15 16p teacher
<Teacher id: 3, name: "Kampachi", subject: "science">
発生している問題・エラーメッセージ
subjectを更新しようと思い、以下のように記述しました。
Ruby
1require 'active_record' 2 3ActiveRecord::Base.establish_connection( 4 adapter: "postgresql", 5 host: "", 6 username: "kampachi", 7 password: "", 8 database: "testdb01" 9) 10 11class Teacher < ActiveRecord::Base 12end 13 14teacher = Teacher.find_by(name: "Kampachi") 15 16teacher.update(subject: "English")
Traceback (most recent call last): 56: from rb_pg_01.rb:30:in `<main>' 55: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/relation.rb:342:in `update' 54: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/relation/delegation.rb:71:in `each' 53: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/relation/delegation.rb:71:in `each' 52: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/relation.rb:342:in `block in update' 51: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/persistence.rb:426:in `update' 50: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:385:in `with_transaction_returning_status' 49: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:212:in `transaction' 48: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:267:in `transaction' 47: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:236:in `within_new_transaction' 46: from /usr/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize' 45: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:239:in `block in within_new_transaction' 44: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:267:in `block in transaction' 43: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:387:in `block in with_transaction_returning_status' 42: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/persistence.rb:428:in `block in update' 41: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/suppressor.rb:44:in `save' 40: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:309:in `save' 39: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:325:in `rollback_active_record_state!' 38: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:310:in `block in save' 37: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:385:in `with_transaction_returning_status' 36: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:212:in `transaction' 35: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:265:in `transaction' 34: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:387:in `block in with_transaction_returning_status' 33: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:310:in `block (2 levels) in save' 32: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/validations.rb:46:in `save' 31: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/persistence.rb:275:in `save' 30: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/callbacks.rb:342:in `create_or_update' 29: from /var/lib/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:816:in `_run_save_callbacks' 28: from /var/lib/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:98:in `run_callbacks' 27: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/callbacks.rb:342:in `block in create_or_update' 26: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/persistence.rb:705:in `create_or_update' 25: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/timestamp.rb:114:in `_update_record' 24: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/callbacks.rb:350:in `_update_record' 23: from /var/lib/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:816:in `_run_update_callbacks' 22: from /var/lib/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:98:in `run_callbacks' 21: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/callbacks.rb:350:in `block in _update_record' 20: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/attribute_methods/dirty.rb:134:in `_update_record' 19: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/persistence.rb:719:in `_update_record' 18: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/locking/optimistic.rb:80:in `_update_row' 17: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/persistence.rb:696:in `_update_row' 16: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/persistence.rb:197:in `_update_record' 15: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/query_cache.rb:21:in `update' 14: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:170:in `update' 13: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:94:in `exec_delete' 12: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `execute_and_clear' 11: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:609:in `exec_no_cache' 10: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:571:in `log' 9: from /var/lib/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/notifications/instrumenter.rb:23:in `instrument' 8: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:580:in `block in log' 7: from /usr/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize' 6: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:581:in `block (2 levels) in log' 5: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:610:in `block in exec_no_cache' 4: from /var/lib/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads' 3: from /var/lib/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares' 2: from /var/lib/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads' 1: from /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:611:in `block (2 levels) in exec_no_cache' /var/lib/gems/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:611:in `async_exec_params': PG::SyntaxError: ERROR: zero-length delimited identifier at or near """" (ActiveRecord::StatementInvalid) LINE 1: ...PDATE "teachers" SET "subject" = $1 WHERE "teachers"."" = $2 ^ : UPDATE "teachers" SET "subject" = $1 WHERE "teachers"."" = $2
SQLを直接操作するときに書くSQL文は以下のようになると思うのですが、
PostgreSQL
1UPDATE teachers SET subject = 'English' WHERE teachers.name = 'kampachi';
WHERE以下にカラムの指定ができていない原因はなんでしょうか?
詳しい方ご教示願います。
バージョン情報
Ruby 2.5.1
activerecord 5.2.3
PostgreSQL 10.8
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。