vendredi 26 février 2016

Rails simple model attributes not saved to database

I am developing a small Rails 3 app that reads data from the Steam API. What I am seeing is that the model attributes for any ActiveRecord model I create are not saved to the database nor are they output if I debug them.

Here my sample model

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

With the migration file being:

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

Here are few tests with the Rails console, I get the same results when I try them in a controller and inspect or yaml the model:

irb(main):001:0> temp = NonPlayableApp.new( steam_id: 33333, name: "Testing" )
=> #<NonPlayableApp id: nil, name: nil, steam_id: nil, created_at: nil, updated_at: nil>
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
=> #<NonPlayableApp id: 64, name: nil, steam_id: nil, created_at: "2016-02-26 19:40:37", updated_at: "2016-02-26 19:40:37">
irb(main):007:0> temp2 = NonPlayableApp.first
  NonPlayableApp Load (1.6ms)  SELECT "non_playable_apps".* FROM "non_playable_apps" LIMIT 1
=> #<NonPlayableApp id: 64, name: nil, steam_id: nil, created_at: "2016-02-26 19:40:37", updated_at: "2016-02-26 19:40:37">
irb(main):008:0> temp2.steam_id
=> nil
irb(main):009:0> temp2.name
=> nil

What am I missing?

Aucun commentaire:

Enregistrer un commentaire