保存在Rails控制台中不起作用

时间:2016-10-18 02:53:31

标签: ruby-on-rails ruby

我有一个非常基本的表叫做radio_inventories

 create_table "radio_inventories", id: false, force: true do |t|
  t.integer  "radio_id"
  t.string   "location"
  t.string   "distributor"
  t.string   "radio_user"
  t.datetime "created_at"
  t.datetime "updated_at"
 end

add_index "radio_inventories", ["radio_id"], name: 
"index_radio_inventories_on_radio_id"

还有一个非常简单的模型:

class RadioInventory < ActiveRecord::Base
  self.table_name = 'radio_inventories'
end

出于某种原因,当我使用rails控制台创建这些模型的新实例并保存它们时,它可以保存得很好

irb(main):001:0> x = RadioInventory.new
=> #<RadioInventory radio_id: nil, location: nil, distributor: nil, radio_user: nil, created_at: nil, updated_at: nil>
irb(main):002:0> x.radio_id = 123
=> 123
irb(main):003:0> x.save
(0.0ms)  begin transaction
SQL (1.0ms)  INSERT INTO "radio_inventories" ("created_at", "radio_id", "updated_at") VALUES (?, ?, ?)  [["created_at", "2016-10-18 02:31:26.487353"], ["radio_id", 123], ["updated_at", "2016-10-18 02:31:26.487353"]]
(41.0ms)  commit transaction
=> true

但是当我尝试更新记录时,我收到了一个错误。当我使用.save,.update或.update_attributes时会发生这种情况。

irb(main):004:0> x = RadioInventory.first
RadioInventory Load (0.0ms)  SELECT  "radio_inventories".* FROM "radio_inventories"  LIMIT 1
=> #<RadioInventory radio_id: 8565, location: nil, distributor: nil, radio_user: nil, created_at: "2016-10-18 02:22:59", updated_at: "2016-10-18 02:22:59">
irb(main):005:0> x.location = 'IT'
=> "IT"
irb(main):006:0> x.save
  (0.0ms)  begin transaction
  (0.0ms)  rollback transaction
TypeError: nil is not a symbol
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activemodel-4.1.8/lib/active_model/dirty.rb:162:in `attribute_was'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/attribute_methods/primary_key.rb:44:in `id_was'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/persistence.rb:494:in `_update_record'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/locking/optimistic.rb:70:in `_update_record'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/attribute_methods/dirty.rb:83:in `_update_record'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/callbacks.rb:310:in `block in _update_record'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/callbacks.rb:82:in `run_callbacks'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/callbacks.rb:310:in `_update_record'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/timestamp.rb:70:in `_update_record'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/persistence.rb:483:in `create_or_update'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/callbacks.rb:302:in `block in create_or_update'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activesupport-4.1.8/lib/active_support/callbacks.rb:82:in `run_callbacks'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/callbacks.rb:302:in `create_or_update'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/persistence.rb:103:in `save'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/validations.rb:51:in `save'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/attribute_methods/dirty.rb:21:in `save'
... 2 levels...
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/transactions.rb:208:in `transaction'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/transactions.rb:268:in `block in save'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/transactions.rb:283:in `rollback_active_record_state!'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/transactions.rb:267:in `save'
    from (irb):6
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/railties-4.1.8/lib/rails/commands/console.rb:90:in `start'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/railties-4.1.8/lib/rails/commands/console.rb:9:in `start'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:69:in `console'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/railties-4.1.8/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'irb(main):007:0>

我也是这个名为“RadioStatus / radio_statuses”的模型/表也失败了。但是当我尝试使用名为'RadioTracker / radio_trackers'的模型/表时,一切正常并且符合预期。
任何有关ActiveRecord以这种方式行为的见解都将非常感激。

2 个答案:

答案 0 :(得分:1)

当表没有默认的主ID时,当我们尝试更新ActiveRecord对象时,Rails会抛出此错误。因此,您可以尝试将self.primary_key = 'radio_id'放在RadioInventory模型类的开头。

我在迁移过程中看到id: false,所以我猜这就是问题所在。

答案 1 :(得分:0)

您应该在update_attributes上分配值:

x= RadioInventory.first
x.update_attributes(location: 'IT')