Is there a way to automatically typecast values that are stored using ActiveRecord::Base.store?
Take this totally impractical example:
class User < ActiveRecord::Base
store :settings, accessors: [ :age ]
end
user = User.new(age: '10')
user.age # => '10'
I know I can just override the reader method for age to convert it to an integer, but I was curious if there was an undocumented way of doing it.
Trying to avoid this:
class User < ActiveRecord::Base
store :settings, accessors: [ :age ]
def age
settings[:age].to_i
end
end
user = User.new(age: '10')
user.age # => 10
Update
Looking for something like:
class User < ActiveRecord::Base
store :settings, accessors: {:age => :to_i}
end
Or:
class User < ActiveRecord::Base
store :settings, accessors: {:age => Integer}
end
As of Rails 3.2.7 there is not a way to automatically typecast values. I’ll update this question if I ever come across a way to do it :/