2

So, I'm building an API wrapper gem that works with Spree's Product model. The API provides extensive customization of the data you send to it. I would like a user of the gem to be able to take advantage of all the customizations should they desire to. This appears to me as if I should create a migration that adds about 50 new attributes to the Product model in order to allow the user to customize these attributes for a product.

It feels awfully invasive for me to add 50 attributes to a model in which only a few may be used by any given user (it would be an edge case for someone to use most/all of them). What's the best practice for offering these attributes to the user without muddying up the main model.

My thought may have been to create another model that belongs to the Product and contains the API attributes, but this seems like it may create technical overhead.

For example:

class Spree::Product
  has_one :spree_product_api_attribute_set
  delegate :attr1, :attr2, :attr3..., to: :spree_product_api_attribute_set
end
8
  • This feels like an array, not 50 individual attributes. Could it be done with an array of attributes? Commented Jan 26, 2015 at 19:50
  • @RobertHarvey how do you mean? You mean persist a Hash?
    – Kyle Macey
    Commented Jan 26, 2015 at 19:53
  • @RobertHarvey it's certainly feasible, but since I'm using MySQL, I'll lost the ability to query by any of those attributes and will also lose ActiveRecord's built-in validations...\
    – Kyle Macey
    Commented Jan 26, 2015 at 19:54
  • Ah, I see. But are these user-defined attributes? How do you know what the validations are going to be if you don't know what the attributes are yet? Commented Jan 26, 2015 at 19:57
  • @RobertHarvey No, they're attributes that the API supports that aren't necessarily native to the engine.
    – Kyle Macey
    Commented Jan 26, 2015 at 20:19

1 Answer 1

1

If you use Postgres, HStore or JSON can store key values pretty easily. You can still use indexes to query the DB. If there were 50 attributes that is what I would use. Far superior to serialized fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.