create table foos (id SERIAL, content VARCHAR(255));
And define a class as follows:
class Foo < ActiveRecord::Base
serialize :content, JSON
end
Creating a new instance of the class gives an error:
f = Foo.new
NoMethodError: undefined method read' for nil:NilClass from /Users/andy/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.0/lib/active_support/whiny_nil.rb:48:inmethod_missing’
from
/Users/andy/Code/FakePlatform/vendor/gems/Darwin/gems/json-1.4.6/lib/json/common.rb:286:in load' from /Users/andy/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/base.rb:1938:inblock in set_serialized_attributes’
create table foos (id SERIAL, content VARCHAR(255));
And define a class as follows:
class Foo < ActiveRecord::Base
serialize :content, JSON
end
the second argument to serialise is used to say that you always want
the serialised object to be of a certain class (e.g. Hash, an
application specific class etc.). I’m not sure what you meant by
passing JSON as that argument but I suspect that it isn’t doing what
you think it is.
the second argument to serialise is used to say that you always want
the serialised object to be of a certain class (e.g. Hash, an
application specific class etc.). I’m not sure what you meant by
passing JSON as that argument but I suspect that it isn’t doing what
you think it is.
I was basing it on this article (as well as a stack overflow answer)
that you can provide a custom serializer in Rails 3.1:
JSON provides a dump and load method on the class so it should be
acceptable as the serializer.
Is this article incorrect or my understanding of it?
I was basing it on this article (as well as a stack overflow answer) that you
can provide a custom serializer in Rails 3.1:
JSON provides a dump and load method on the class so it should be acceptable as
the serializer.
Is this article incorrect or my understanding of it?
Having just had a read of ActiveRecord::Base, I still can’t see how I go
wrong
Base#serialize (line 557 of base.rb in ActiveRecord 3.1.0)
if the object supplied as the second argument responds to :load and
:dump (which JSON does), then it uses that object, if not it wraps it in
a YAMLColumn and sets the entry in serialized_attributes to that object.
Then in:
Base#arel_attributes_values (line 1963 of base.rb in ActiveRecord 3.1.0)
if there is an entry in serialized_attributes for a given attribute name
(with a value put in to the coder variable) it calls coder.dump with the
attribute value.
So, I can’t understand why JSON wouldn’t work as a second parameter…