0

I have a table/class :

class User < ActiveRecord::Base
    serialize :photos
end

When I query, via something like this:

Post.joins(:user).select('users.username, users.photo AS user_photo')

This returns the photo, but not as a 'hash' but rather as the raw string stored in the database:

{
    "post_type" = 0;
    "user_id" = 1;
    "user_photo" = "---\n:photo: http://res.cloudinary.com/jhess2991/image/upload/eb29c343249624.png\n:thumb: http://res.cloudinary.com/jhess2991/image/upload/c_scale,h_180,w_180/eb29c3423429624.png\n";
    username = HDILOfficial;
}

Of course, this only happens when I query from a ActiveRecord class other than User. If I query from User (User.select(blah blah blah)) then it returns as a normal hash.

So my question is, how can I make it treat 'photo' as a hash when querying from another class?

2

2 Answers 2

0

At the time being, you can't. There is no way for ActiveRecord to know that the user_photo references the value of the photos attribute you defined in the User model.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok I understand it's not possible. But not sure about the 'no way it can know' part. Are you sure there's no way for AR to know that it's doing a joins on a table that has a serialize attribute? :)
At first glance, I assume it will not be possible without some hacks. I can't see any easy way to accomplish it.
-1

Did you see the information an api.rubyonrails.org

http://api.rubyonrails.org/classes/ActiveRecord/Base.html#label-Saving+arrays%2C+hashes%2C+and+other+non-mappable+objects+in+text+columns

Saving arrays, hashes, and other non-mappable objects in text columns

Active Record can serialize any object in text columns using YAML. To do so, you must specify this with a call to the class method serialize. This makes it possible to store arrays, hashes, and other non-mappable objects without doing any additional work.

class User < ActiveRecord::Base
  serialize :preferences
end

user = User.create(preferences: { "background" => "black", "display" => large })
User.find(user.id).preferences # => { "background" => "black", "display" => large }

You can also specify a class option as the second parameter that'll raise an exception if a serialized object is retrieved as a descendant of a class not in the hierarchy.

class User < ActiveRecord::Base
  serialize :preferences, Hash
end

user = User.create(preferences: %w( one two three ))
User.find(user.id).preferences    # raises SerializationTypeMismatch

When you specify a class option, the default value for that attribute will be a new instance of that class.

class User < ActiveRecord::Base
  serialize :preferences, OpenStruct
end

user = User.new
user.preferences.theme_color = "red"

This may helps

1 Comment

@0xSina sorry, where is your question. btw i have a kind of holiday :) it takes some time. i am away till 6. jan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.