4

I am working on upgrading rails application, I have a method unserialize_attribue in rails 3 but I am not able to see this method in rails 5.

What is the alternative for unserialize_attribute in rails 5 or any suggestion on what can I do, Thanks

Braham Shakti
  • 1,408
  • 4
  • 22
  • 39
  • How do you want your unserialised attribute to look like? – ray Jan 15 '19 at 07:17
  • How does your unserialised attribute look like in rails 3? – Dyaniyal Wilson Jan 17 '19 at 05:27
  • I am trying to update to rails 3.2.22 from 3.1.12, but still getting error of `vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.22.5/lib/active_support/core_ext/module/aliasing.rb:31:in 'alias_method': undefined method 'unserialize_attribute' for class `ActiveRecord::Base' (NameError)`. Can you help me with this? – Vishal Feb 11 '20 at 06:09

2 Answers2

5

unserialize_attribute is removed from rails-5 but you can unserialized value in different way,

Method:1

> @user.accessible_attribute
# => {"attr1"=>["Email id"], "attr2"=>["First Name"], "attr3"=>["Last Name"]}

> @user.attributes_before_type_cast['accessible_attribute']
# => "--- !ruby/hash:ActionController::Parameters\nattr1:\n- Email id\nattr2:\n- First Name\nattr3:\n- Last Name\n"

Method:2

@user.instance_variable_get(:@attributes)['accessible_attribute'].value_before_type_cast
# => "--- !ruby/hash:ActionController::Parameters\nattr1:\n- Email id\nattr2:\n- First Name\nattr3:\n- Last Name\n"

Method:3

> @accessible_attribute = ActiveRecord::Base.connection.execute("SELECT accessible_attribute FROM users WHERE id = #{@user.id}")
   (0.4ms)  SELECT accessible_attribute FROM users WHERE id = 3
# => #<Mysql2::Result:0xe0fc374 @query_options={:as=>:array, :async=>false, :cast_booleans=>false, :symbolize_keys=>false, :database_timezone=>:utc, :application_timezone=>nil, :cache_rows=>true, :connect_flags=>-2147442171, :cast=>true, :default_file=>nil, :default_group=>nil, :adapter=>"mysql2", :encoding=>"utf8mb4", :collation=>"utf8mb4_unicode_ci", :database=>"example_development", :pool=>5, :username=>"root", :password=>"password", :socket=>"/var/run/mysqld/mysqld.sock", :flags=>2}>

> @accessible_attribute.first[0]
# => "--- !ruby/hash:ActionController::Parameters\nattr1:\n- Email id\nattr2:\n- First Name\nattr3:\n- Last Name\n"

Note: Precisely, Some prefer to take attribute value unserialized by @user.accessible_attribute.to_yaml also.

ray
  • 5,454
  • 1
  • 18
  • 40
0

I got it working using serializable_hash and it is available in rails 3 as well as in rails 5

I have this code written in rails 3

unserialized_value = unserialize_attribute(options[:serialized_column]) || { }

which I changed to

unserialized_value = self.serializable_hash[options[:serialized_column]] || { }

both method returns the same hash object.

Braham Shakti
  • 1,408
  • 4
  • 22
  • 39
  • I am not clear how it can be solution for question you asked, but glad that it worked for you. If you explain how many changes you did, it will be great – ray Jan 22 '19 at 06:00