0

I have a model called Page. The users are able to specify attributes whenever they create a Page. Ideally, they will choose from a list of attributes and assign them to the Page.

My first though was to create as many columns as attributes the user is able to select, and basically marking them as true/false if it has been selected.

However, I have a feeling that I might be missing a better approach. I have also used at HSTORE, but at the end of the day, I would also need to do something like:

attributes: { 'attribute1' => 'true', 'attribute2' => 'false' } and I am not sure if that is appropriate.

I will use those attributes in order to select pages, so I should be able to say something like:

Page.where(attribute1 is true).where(attribute2 is false).where(...)

How should I store those attributes in my Page model?

4
  • don't you want to store attributes in a separate table with a many-to-many relationship with Pages? and when grabbing the result you'd just check whether relation row exists or not. Commented Aug 26, 2014 at 16:53
  • The JSON storage system in Postgres 9.3 allows for querying on these. Is that an option for you? Commented Aug 26, 2014 at 16:53
  • tadman, it is an option. I am ignorant on how would I use it on order to add/remove attributes though. Also, I am ignorant in knowing if that would be the best option for this, or if having several columns would still be better? Commented Aug 26, 2014 at 16:56
  • tadman, the way I imagine this (visually) is a list of attributes, where the user can select (checkbox) and the ones selected will be applied to the Page. If that helps on explaining the data model. Commented Aug 26, 2014 at 17:03

1 Answer 1

0

The acts_as_taggable_on gem might be of help to you.

You would declare your model as taggable on your attributes as follows:

class Page < ActiveRecord::Base
  acts_as_ordered_taggable_on :attributes
end

And your example query would be something like this:

Page.tagged_with(["attribute1"]).tagged_with(["attribute2"], :exclude => true)

The Gem is very well documented here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.