The ActiveRecord::Store was introduced in Rails 3.2
The ActiveRecord::Store gives us the ability to store a hash to our database cloumn as serialized data.
store allows to declare store accessors, which acts as any other attribute of model, we have to declare the database column used for the store as a text, which gives us enough room to store our data in database.
How to use it :
class Test < ActiveRecord::Base
store :setting_store, accessors: [:cost]
end
temp= Test.new
=>#<Test id: nil, setting_store: {}, created_at: nil, updated_at: nil>
As we can see there is empty setting_store hash.
lets set some value into it.
temp.cost = 150
=>150
we have store value to our setting_store hash let's check it.
temp
=>#<Test id: nil, setting_store: {:cost=>150}, created_at: nil, updated_at: nil>
lets access by its accessors
temp.cost
=>150
very nice..
ReplyDeleteThanks @Ganesh
ReplyDelete