If I have several objects that each have basically a Profile
, what I'm using to store random attributes, what are the pros and cons of:
- Storing a serialized hash in a column for a record, vs.
- Storing a bunch of key/value objects that
belong_to
the main object.
Code
Say you have STI records like these:
class Building < ActiveRecord::Base
has_one :profile, :as => :profilable
end
class OfficeBuilding < Building; end
class Home < Building; end
class Restaurant < Building; end
Each has_one :profile
Option 1. Serialized Hash
class SerializedProfile < ActiveRecord::Base
serialize :settings
end
create_table :profiles, :force => true do |t|
t.string :name
t.string :website
t.string :email
t.string :phone
t.string :type
t.text :settings
t.integer :profilable_id
t.string :profilable_type
t.timestamp
end
Option 2. Key/Value Store
class KeyValueProfile < ActiveRecord::Base
has_many :settings
end
create_table :profiles, :force => true do |t|
t.string :name
t.string :website
t.string :email
t.string :phone
t.string :type
t.integer :profilable_id
t.string :profilable_type
t.timestamp
end
create_table :settings, :force => true do |t|
t.string :key
t.text :value
t.integer :profile_id
t.string :profile_type
t.timestamp
end
Which would you choose?
Assume that 99% of the time I won't need to search by the custom settings
. Just wondering what the tradeoffs are in terms of performance and the likelihood of future problems. And the number of custom settings
will likely be anywhere from 10-50.
I would rather go with the second option, with the settings table, because it follows the ActiveRecord object-oriented conventions. But I'm wondering if in this kind of situation that would come at too high a performance cost.
Note: I am wondering in terms of RDBMS only. This would be a perfect fit for MongoDB/Redis/CouchDB/etc. but I want to know purely the pros and cons in terms of SQL.