Search code examples
ruby-on-railsapache-flexweblogiclogic

Best logic for storing categories of images


I am creating a system where a user can select any combination of four options.

Then results are displayed based on their exact combination.

For example, if a user chose color, text, and logo. Only results featuring color, text, and logo would return.

On the other side, I am creating "templates" that will eventually be the results returned. Each template will be earmarked to be able to return properly when a user selects a combination that corresponds with the template.

My Question :

What is the best way to categorize this information in the back-end so it can be pulled by the user requests?

For example, I have a template that can be color and text OR color, text, and logo. My guess is two group these two in a family, and then when a combination is made, a query reviews each family for a matching combo; if true, that specific combo variation is returned.

Would you do it any differently?

Thanks!


Solution

  • You shoudn't hardcode the number of available categories (what if tomorrow you need another category?)

    Combination
      has_many :categories
      belongs_to :template
    
      def self.find_by_selected_categories(array_of_categories)
        self.find(:first, :conditions => {:categories => array_of_categories})
      end
    end
    
    Template
      has_many: combinations
    end
    

    ...

    The downside of this approach is that you must have a correspondence table (categories-combinations).