Hello team,
First off thanks for writing this gem, we use the 0.8.1 version in our app and it works great for us.
This is some strange error which we just start getting on production and will try my best to explain.
Code for one serializer
class Api::V1::OrganizationSerializer < ActiveModel::Serializer
attributes :achievemtents, :templates
def achievements
object.achievements_summary
end
def templates
object.templates_summary
end
end
Both method achievements_summary and templates_summary are custom methods which give array of hashes as output.
Output response locally for serializer:
{
"id": 1,
"achievements": [{
"achievement_id": "examplecourse1",
"count": 4,
"credential_name": "Example Course"
}],
"templates": [{
"template_name": "sample template",
"template_id": 3,
"count": 0
}]
}
Now on production we get this error for the same:
undefined method `read_attribute_for_serialization' for {:template_name=>"ftft", :template_id=>1, :count=>1}:Hash
from /usr/local/share/ruby/gems/2.0/gems/active_model_serializers-0.8.1/lib/active_model/serializer.rb:99:in `block in attribute'
if i comment the second method templates_summary the serializer works fine on production also and give this output:
{
"id": 1,
"achievements": [{
"achievement_id": "examplecourse1",
"count": 4,
"credential_name": "Example Course"
}]
}
Now few question which I was not able to find answers are:
1) Both on production we uses the same version of serializer 0.8.1 so why this error?
2) In the achievements_custom template also we have a custom array of hashes so why not this error comes on production with that method also?
Ruby version on both: ruby 2.0.0p451 (2014-02-24 revision 45167) [x86_64-linux]
Rails version on both: 4.1.0
I am not really expecting an accurate answer for this, but any good start/advice will also be great for me to put more brain in it.
thanks
Could not reproduce the error with ruby 2.0.0, rails 4.1.0 and active_model_serializers 0.8.1.
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
ruby '2.0.0'
gem 'rails' , '4.1.0'
gem 'arel'
gem 'sqlite3'
gem 'active_model_serializers', '0.8.1'
gem 'pry'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'active_model_serializers'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.string :title
t.text :body
end
end
class Post < ActiveRecord::Base
validates :title, :body, presence: true
def achievements_summary
[{ achievement_id: "examplecourse1", count: 4, credential_name: "Example Course" }]
end
def templates_summary
[{ template_name: "sample template", template_id: 3, count: 0 }]
end
end
class PostSerializer < ActiveModel::Serializer
attributes :achievements, :templates
def achievements
object.achievements_summary
end
def templates
object.templates_summary
end
end
class BugTest < Minitest::Test
def test_json
post = Post.create!(title: 'Test filter', body: 'Test if the filter method is working fine')
assert_equal PostSerializer.new(post).to_json, {post: {achievements: post.achievements_summary, templates: post.templates_summary}}.to_json
end
end
My guess it one of the objects doesn't include ActiveModel::Serialization http://api.rubyonrails.org/classes/ActiveModel/Serialization.html
Propose closing as stale. I'm happy to add a test and docs for this scenario if there's a desire.
@bf4 tests are always welcome! Indeed I'm closing it
Most helpful comment
My guess it one of the objects doesn't include
ActiveModel::Serializationhttp://api.rubyonrails.org/classes/ActiveModel/Serialization.html