Browse files

Serialized attribute can be serialized in an integer column

Fix #8575
  • Loading branch information...
1 parent aefcd34 commit 28a25ac811683adc703218b2d1bfe3a1a9cd84c4 @rafaelfranca committed Dec 21, 2012
View
8 activerecord/lib/active_record/attribute_methods/serialization.rb
@@ -112,6 +112,14 @@ def type_cast_attribute_for_write(column, value)
end
end
+ def _field_changed?(attr, old, value)
+ if self.class.serialized_attributes.include?(attr)
+ old != value
+ else
+ super
+ end
+ end
+
def read_attribute_before_type_cast(attr_name)
if self.class.serialized_attributes.include?(attr_name)
super.unserialized_value
View
11 activerecord/test/cases/serialized_attribute_test.rb
@@ -1,5 +1,6 @@
-require "cases/helper"
+require 'cases/helper'
require 'models/topic'
+require 'models/person'
require 'bcrypt'
class SerializedAttributeTest < ActiveRecord::TestCase
@@ -225,4 +226,12 @@ def test_serialize_attribute_via_select_method_when_time_zone_available
ensure
ActiveRecord::Base.time_zone_aware_attributes = false
end
+
+ def test_serialize_attribute_can_be_serialized_in_an_integer_column
+ insures = ['life']
+ person = Person.new(first_name: 'David', insures: insures)
+ assert person.save
+ person = person.reload
+ assert_equal(true, person.insures == insures, 'password should equal')
+ end
end
View
16 activerecord/test/models/person.rb
@@ -1,4 +1,20 @@
+class Insure
+ INSURES = %W{life annuality}
+
+ def self.load mask
+ INSURES.select do |insure|
+ (1 << INSURES.index(insure)) & mask > 0
+ end
+ end
+
+ def self.dump insures
+ numbers = insures.compact.uniq.collect{|insure|INSURES.index(insure)}.compact
+ numbers.inject(0){|sum,n| sum + (1<<n)}
+ end
+end
+
class Person < ActiveRecord::Base
+ serialize :insures, Insure
has_many :readers
has_many :secure_readers
has_one :reader
View
1 activerecord/test/schema/schema.rb
@@ -494,6 +494,7 @@ def create_table(*args, &block)
t.integer :followers_count, :default => 0
t.references :best_friend
t.references :best_friend_of
+ t.integer :insures, null: false, default: 0
t.timestamps
end

0 comments on commit 28a25ac

Please sign in to comment.