Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access column value after serialization activerecord

I have a simple model like this:

class User < ActiveRecord::Base
  serialize :preferences
end

I want to access the raw value from mysql, not value before serialize. Is it possible?

I know I can use

ActiveRecord::Base.connection.execute("select * from users")

But I want to access from the User model.

like image 694
Chamnap Avatar asked Jun 14 '12 15:06

Chamnap


1 Answers

attributes_before_type_cast didn't work for me.

User.first.instance_variable_get(:@attributes)['preferences'].serialized_value

This works even if the object is loaded.

like image 62
YasirAzgar Avatar answered Sep 25 '22 10:09

YasirAzgar