1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Serialized attribute can be serialized in an integer column

Fix #8575
This commit is contained in:
Rafael Mendonça França 2012-12-21 14:15:33 -03:00
parent aefcd342c4
commit f2e0925ab7
5 changed files with 47 additions and 3 deletions

View file

@ -1,12 +1,17 @@
## Rails 4.0.0 (unreleased) ##
* Serialized attributes can be serialized in integer columns.
Fix #8575.
*Rafael Mendonça França*
* Keep index names when using `alter_table` with sqlite3.
Fix #3489
Fix #3489.
*Yves Senn*
* Add ability for postgresql adapter to disable user triggers in `disable_referential_integrity`.
Fix #5523
Fix #5523.
*Gary S. Weaver*

View file

@ -112,6 +112,14 @@ module ActiveRecord
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 file

@ -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 @@ class SerializedAttributeTest < ActiveRecord::TestCase
ensure
ActiveRecord::Base.time_zone_aware_attributes = false
end
def test_serialize_attribute_can_be_serialized_in_an_integer_column
insures = ['life']
person = SerializedPerson.new(first_name: 'David', insures: insures)
assert person.save
person = person.reload
assert_equal(insures, person.insures)
end
end

View file

@ -100,3 +100,24 @@ class NestedPerson < ActiveRecord::Base
assign_attributes({ :best_friend_attributes => { :first_name => new_name } })
end
end
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.map { |insure| INSURES.index(insure) }
numbers.inject(0) { |sum, n| sum + (1 << n) }
end
end
class SerializedPerson < ActiveRecord::Base
self.table_name = 'people'
serialize :insures, Insure
end

View file

@ -494,6 +494,7 @@ ActiveRecord::Schema.define do
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