Rails简单模型属性未保存到数据库

我正在开发一个小型Rails 3应用程序,用于从Steam API读取数据。 我所看到的是我创建的任何ActiveRecord模型的模型属性都没有保存到数据库中,如果我debug它们也不输出。

这是我的样本模型

 class NonPlayableApp < ActiveRecord::Base attr_accessor :name, :steam_id attr_accessible :name, :steam_id end 

迁移文件为:

 class CreateNonPlayableApps < ActiveRecord::Migration def change create_table :non_playable_apps do |t| t.string :name t.string :steam_id t.timestamps end end end 

以下是使用Rails控制台进行的一些测试,当我在控制器中尝试它们并inspect或模拟模型时,我得到相同的结果:

 irb(main):001:0> temp = NonPlayableApp.new( steam_id: 33333, name: "Testing" ) => # irb(main):002:0> temp.steam_id => 33333 irb(main):003:0> temp.name => "Testing" irb(main):004:0> temp.save (0.1ms) begin transaction SQL (4.0ms) INSERT INTO "non_playable_apps" ("created_at", "name", "steam_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Fri, 26 Feb 2016 19:40:37 UTC +00:00], ["name", nil], ["steam_id", nil], ["updated_at", Fri, 26 Feb 2016 19:40:37 UTC +00:00]] (8.8ms) commit transaction => true irb(main):005:0> temp.steam_id => 33333 irb(main):006:0> temp => # irb(main):007:0> temp2 = NonPlayableApp.first NonPlayableApp Load (1.6ms) SELECT "non_playable_apps".* FROM "non_playable_apps" LIMIT 1 => # irb(main):008:0> temp2.steam_id => nil irb(main):009:0> temp2.name => nil 

我错过了什么?

attr_accessor :name, :steam_id会覆盖Rails为该属性自动生成的setter和getter方法。

只需从模型中删除attr_accessor :name, :steam_id行。