In my Rails3 app, I am using ActiveRecord and Postgresql.
Say I have a model called Parts. The model has a small list of standard attributes such as price, quantity, etc.
However, Customer A might want to add LotNumber and CustomerB might want to add OriginalLocation.
How would I allow them to do that?
I thought about creating a PartsDetail model that allowed them to have a type.
class PartsDetail < ActiveRecord::Base
attr_accessible :type, :value, :part_id
belongs_to :parts
end
So that “type” could be “LotNumber”, etc.
But I’m not quite sure how that would work in my associations and querying.
Any ideas?
Thanks.
Since you’re using PostgreSQL, you could use
hstoreto store arbitrary hashes in database columns:There’s even a gem for adding
hstoresupport to ActiveRecord:Then you could create an
hstorecolumn called, say,client_specificand look inside it with things like:to see which clients have noted that they like pancakes.
You might want to store a list of customer-specific fields somewhere with the customer record to make the UI side of things easier to deal with but that’s pretty simple to do.