UPDATED:
Exact implementation will depend on your database, but PostgreSQL now has json
and jsonb
columns which can natively store your hash/object data and allow you to query against the JSON with ActiveRecord!
change your migration and you're done.
class Migration0001
def change
add_column :users, :location_data, :json, default: {}
end
end
ORIGINAL:
For more details: rails docs && apidock
Make sure your column is :text
and not :string
Migration:
$ rails g migration add_location_data_to_users location_data:text
should create:
class Migration0001
def change
add_column :users, :location_data, :text
end
end
Your Class Would Look Like:
class User < ActiveRecord::Base
serialize :location_data
end
Available Actions:
b = User.new
b.location_data = [1,2,{foot: 3, bart: "noodles"}]
b.save
More Awesome?!
utilize postgresql hstore
class AddHstore < ActiveRecord::Migration
def up
enable_extension :hstore
end
def down
disable_extension :hstore
end
end
class Migration0001
def change
add_column :users, :location_data, :hstore
end
end
With hstore you can set attributes on the serialized field
class User < ActiveRecord::Base
# setup hstore
store_accessor :location_data, :city, :state
end