ruby - Rails save serialized object fails? -


see following output:

1.9.3p194 :001 > player = player.randomize_for_market  => #<player id: nil, name: "gale bridges", age: 19, energy: 100, attack: 6, defense: 4, stamina: 5, goal_keeping: 3, power: 4, accuracy: 5, speed: 5, short_pass: 5, ball_controll: 4, long_pass: 6, regain_ball: 5, contract_id: nil, created_at: nil, updated_at: nil>  1.9.3p194 :002 > player.save!    (0.2ms)  begin    sql (20.5ms)  insert "players" ("accuracy", "age", "attack", "ball_controll", "contract_id", "created_at", "defense", "energy", "goal_keeping", "long_pass", "name", "power", "regain_ball", "short_pass", "speed", "stamina", "updated_at") values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) returning "id"  [["accuracy", 5], ["age", 19], ["attack", 6], ["ball_controll", 4], ["contract_id", nil], ["created_at", fri, 29 jun 2012 04:02:34 utc +00:00], ["defense", 4], ["energy", 100], ["goal_keeping", 3], ["long_pass", 6], ["name", "gale bridges"], ["power", 4], ["regain_ball", 5], ["short_pass", 5], ["speed", 5], ["stamina", 5], ["updated_at", fri, 29 jun 2012 04:02:34 utc +00:00]]    (16.6ms)  commit  => true  1.9.3p194 :003 > yaml::load(yaml::dump(player.randomize_for_market)).save!    (0.2ms)  begin    (0.2ms)  commit  => true 

why happens , how can avoid it?

there no ((before|after)+(save|create|commit)) on model. i'm using rails 3.2.

                                   table "public.players"    column     |            type             |                      modifiers                        --------------+-----------------------------+------------------------------------------------------ id            | integer                     | not null default nextval('players_id_seq'::regclass) name          | character varying(255)      | not null age           | integer                     | not null energy        | integer                     | not null attack        | integer                     | not null defense       | integer                     | not null stamina       | integer                     | not null goal_keeping  | integer                     | not null power         | integer                     | not null accuracy      | integer                     | not null speed         | integer                     | not null short_pass    | integer                     | not null ball_controll | integer                     | not null long_pass     | integer                     | not null regain_ball   | integer                     | not null contract_id   | integer                     |  created_at    | timestamp without time zone | not null updated_at    | timestamp without time zone | not null  indexes:    "players_pkey" primary key, btree (id) 

edit: answering "why expect yaml::load(yaml::dump(player.randomize_for_market)).save! anything?"

because serializes object , recovers it? example:

1.9.3p194 :006 > p = player.randomize_for_market  => #<player id: nil, name: "vincenzo allen", age: 23, energy: 100, attack: 2, defense: 8, stamina: 6, goal_keeping: 3, power: 5, accuracy: 6, speed: 5, short_pass: 6, ball_controll: 5, long_pass: 6, regain_ball: 5, contract_id: nil, created_at: nil, updated_at: nil>  1.9.3p194 :007 > p  => #<player id: nil, name: "vincenzo allen", age: 23, energy: 100, attack: 2, defense: 8, stamina: 6, goal_keeping: 3, power: 5, accuracy: 6, speed: 5, short_pass: 6, ball_controll: 5, long_pass: 6, regain_ball: 5, contract_id: nil, created_at: nil, updated_at: nil>  1.9.3p194 :008 > yaml::load(yaml::dump(p))  => #<player id: nil, name: "vincenzo allen", age: 23, energy: 100, attack: 2, defense: 8, stamina: 6, goal_keeping: 3, power: 5, accuracy: 6, speed: 5, short_pass: 6, ball_controll: 5, long_pass: 6, regain_ball: 5, contract_id: nil, created_at: nil, updated_at: nil>  

note return of p same of return yaml::load

this may answer question:

:001 > article = article.new #<article:0x102d16b10> { ... } :002 > article.persisted? false :003 > dumped = yaml::dump(article) "--- !ruby/object:article ... " :004 > loaded = yaml::load(dumped) #<article:0x102cf5500> { ... } :005 > loaded.persisted? true 

looking rails source code activerecord::base#persisted?:

def persisted?   !(new_record? || destroyed?) end 

and activerecord::base#new_record?:

def new_record?   @new_record end 

the @new_record instance variable not saved when dump object yaml, , therefore it's nil when load object yaml. activerecord thinks it's been persisted database , doesn't attempt save it.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -