For some reason, whenever I access a serialized attribute from the console, it treats it as a normal string. The serialization/deserialization works fine when not accessed from the console (ex. from the view). I have the code as follows:
class Foo < ActiveRecord::Base
belongs_to :user
serialize :serialized_data # column type is text
...
end
For example, if I were to put in app/views/users/show.html.erb the following:
<%= @user.foo.serialized_data.first %>
It would properly print out the first of the set of data. When I run the following command from console:
> User.first.foo.serialized_data.first
It would simply print out -, which is the first character of a YAML file. Further, User.first.foo.serialized_data would print out a large string rather than the array/hash that was originally saved.
UPDATE:
If I generate the data in :serialized_data and attempt to access it in the same console session, it seems to work fine. Once I close console and then re-enter it, the issue occurs.
EDIT:
For clarification, :serialized_data is an array of hashes in the following form:
[{"stuff" => {"name" => name, "qty" => 1}}, {"stuff" => {"name" => name2, "qty" => 3}}]
and can be accessed as such:
user.foo.serialized_data[0]["stuff"]["qty"]
=> 1
EDIT 2:
It may also be important to note that I am storing a model object in my hash. As mentioned in the comments below, if we were to use the example I gave, user.foo.serialized_data[0]["stuff"]["name"] would return an object of model Name with attributes. So, I could run user.foo.serialize_data[0]["stuff"]["name"].id and so on. Maybe the object is messing with the syntax of the YAML file.
The problem was that I was storing an entire object in the hash value, as mentioned in EDIT 2. Switching it to the object
idfixed this issue.