hardbap (owner)

Revisions

gist: 164770 Download_button fork
public
Public Clone URL: git://gist.github.com/164770.git
Embed All Files: show embed
serialized-attributes-clobbered-on-partial-updates #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2397-serialized-attributes-clobbered-on-partial-updates#ticket-2397-3
class CreatePosts < ActiveRecord::Migration
  def self.up
    create_table :posts do |t|
      t.string :title
      t.text :body
 
      t.timestamps
    end
  end
 
  def self.down
    drop_table :posts
  end
end
 
class Post < ActiveRecord::Base
  serialize :body
end
 
# in the console...
demo[dev]>> p = Post.find(3)
=> #<Post id: 3, title: "you", body: #<YAML::Object:0x23ac648 @class="Feedzirra::Feed", @ivars={}>, created_at: "2009-08-09 13:09:07", updated_at: "2009-08-09 13:20:41"
 
demo[dev]>> p = Post.find(3, :select =>'id, title')
=> #<Post id: 3, title: "you">
 
demo[dev]>> p.update_attribute :title, 'dirty'
=> true
 
demo[dev]>> p = Post.find(3)
=> #<Post id: 3, title: "dirty", body: nil, created_at: "2009-08-09 13:09:07", updated_at: "2009-08-09 13:22:28">
 
# line 142 in dirty.rb
      def update_with_dirty
        if partial_updates?
          # Serialized attributes should always be written in case they've been
          # changed in place.
          update_without_dirty(changed | self.class.serialized_attributes.keys)
        else
          update_without_dirty
        end
      end