Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #15240 from chancancode/fix_attribute_methods

Fixed serialization for records with an attribute named `format`.
  • Loading branch information...
commit 8b364715010c25ddb28500d5b4a612386c437eb3 2 parents 0cc7f02 + 28d52c5
Aaron Patterson tenderlove authored
6 activerecord/CHANGELOG.md
View
@@ -1,3 +1,9 @@
+* Fixed serialization for records with an attribute named `format`.
+
+ Fixes #15188.
+
+ *Godfrey Chan*
+
* When a `group` is set, `sum`, `size`, `average`, `minimum` and `maximum`
on a NullRelation should return a Hash.
1  activerecord/lib/active_record/attribute_methods.rb
View
@@ -66,6 +66,7 @@ def initialize_generated_modules # :nodoc:
# Generates all the attribute related methods for columns in the database
# accessors, mutators and query methods.
def define_attribute_methods # :nodoc:
+ return false if @attribute_methods_generated
# Use a mutex; we don't want two thread simultaneously trying to define
# attribute methods.
generated_attribute_methods.synchronize do
2  activerecord/lib/active_record/core.rb
View
@@ -286,6 +286,8 @@ def init_with(coder)
@new_record = false
+ self.class.define_attribute_methods
+
run_callbacks :find
run_callbacks :initialize
19 activerecord/test/cases/serialization_test.rb
View
@@ -1,8 +1,11 @@
require "cases/helper"
require 'models/contact'
require 'models/topic'
+require 'models/book'
class SerializationTest < ActiveRecord::TestCase
+ fixtures :books
+
FORMATS = [ :xml, :json ]
def setup
@@ -65,4 +68,20 @@ def test_include_root_in_json_allows_inheritance
ensure
ActiveRecord::Base.include_root_in_json = original_root_in_json
end
+
+ def test_read_attribute_for_serialization_with_format_after_init
+ klazz = Class.new(ActiveRecord::Base)
+ klazz.table_name = 'books'
+
+ book = klazz.new(format: 'paperback')
+ assert_equal 'paperback', book.read_attribute_for_serialization(:format)
+ end
+
+ def test_read_attribute_for_serialization_with_format_after_find
+ klazz = Class.new(ActiveRecord::Base)
+ klazz.table_name = 'books'
+
+ book = klazz.find(books(:awdr).id)
+ assert_equal 'paperback', book.read_attribute_for_serialization(:format)
+ end
end
2  activerecord/test/fixtures/books.yml
View
@@ -2,8 +2,10 @@ awdr:
author_id: 1
id: 1
name: "Agile Web Development with Rails"
+ format: "paperback"
rfr:
author_id: 1
id: 2
name: "Ruby for Rails"
+ format: "ebook"
1  activerecord/test/schema/schema.rb
View
@@ -103,6 +103,7 @@ def except(adapter_names_to_exclude)
create_table :books, force: true do |t|
t.integer :author_id
+ t.string :format
t.column :name, :string
t.column :status, :integer, default: 0
t.column :read_status, :integer, default: 0
Please sign in to comment.
Something went wrong with that request. Please try again.