Skip to content

Instantly share code, notes, and snippets.

@AquisTech
Last active October 31, 2018 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AquisTech/021e6dfea598f4fe9726d1430a69088a to your computer and use it in GitHub Desktop.
Save AquisTech/021e6dfea598f4fe9726d1430a69088a to your computer and use it in GitHub Desktop.
Store for serialized text column
class User < ActiveRecord::Base
  store :settings, accessors: [ :color, :homepage ], coder: JSON
end

u = User.new(color: 'black', homepage: '37signals.com')
u.color                          # Accessor stored attribute
u.settings[:country] = 'Denmark' # Any attribute, even if not specified with an accessor

# There is no difference between strings and symbols for accessing custom attributes
u.settings[:country]  # => 'Denmark'
u.settings['country'] # => 'Denmark'

Reference: ActiveRecord Store

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment