【问题标题】:Rails save serialized object fails?Rails 保存序列化对象失败?
【发布时间】:2012-06-30 15:21:37
【问题描述】:

查看以下输出:

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 INTO "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

为什么会发生这种情况,我该如何避免?

模型上没有 ((before|after)+(save|create|commit))。我正在使用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)

编辑:回答“你为什么希望 YAML::load(YAML::dump(Player.randomize_for_market)).save! 做任何事情?”

因为它序列化一个对象并恢复它? 示例:

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> 

注意 p 的返回和 YAML::load 的返回是一样的

【问题讨论】:

  • 你为什么指望YAML::load(YAML::dump(Player.randomize_for_market)).save! 做任何事?
  • 因为它应该序列化和恢复对象?请检查我的编辑并为您解答
  • 但是为什么save! 会对一个实际上并没有改变的对象做任何事情呢?您的 .save! 调用基于返回值、没有异常以及 SQL BEGINCOMMIT 成功;但如果什么都没有改变,那你为什么会期望 save 不是无操作?
  • 既然对象在保存前ID为空,难道不应该得到一个新的并保存在数据库中吗?请注意第一个示例,我给出了第一个生成的播放器是如何保存的,而第二个不是。
  • 但是有相当多的状态不会出现在 YAML 对象中,因此转储/加载之前的内部状态与之后的状态不同。例如,在控制台中获取您的模型实例 (m),更改属性 (a),执行您的 m2 = YAML::load(YAML::dump(m)),然后查看 m2.a_changed? 的内容。也许您应该解释一下您通过 YAML 的往返旅程实际上要完成的工作。

标签: ruby-on-rails ruby activerecord serialization yaml


【解决方案1】:

这可能有助于回答您的问题:

: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

查看ActiveRecord::Base#persisted? 的 Rails 源代码:

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

对于ActiveRecord::Base#new_record?

def new_record?
  @new_record
end

@new_record 实例变量在您将对象转储到 Yaml 时不会保存,因此当您从 Yaml 加载对象时它是 nil。所以 ActiveRecord 认为它已经被持久化到数据库中并且不会尝试保存它。

【讨论】:

  • 是的,YAML 只包含表中的数据,告诉 ActiveRecord 需要对这些数据做什么的重要事情在 YAML 的往返过程中无法生存。 +1 用于追踪更多丢失的部分。
【解决方案2】:

Brandan 的回答非常相关,从 YAML 反序列化的对象认为它已经被持久化了。假设 @loaded_obj 是您从 YAML 加载的对象(您要保存的对象),请尝试 @loaded_obj.dup.save

【讨论】:

  • 在 Rails 4 中,loaded_obj.persisted?=false,这是正确的,但显然代码没有“意识到”任何值已更改,因此它不会保存任何值。使用 dup() 可以解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多