1

Im writting a web-page in Ruby on Rails , so i can learn the language and framework better.

I have a User model which has_many Classifieds models which has_many Photos which accept_attachment :image.

So the whole thing works fine , the User creates the Classified , adds details and photos . User model and Classifieds model CRUD are implemented and running!

Now that the basic functionality is ok , i want to start implementing categorization and search.

One option is to create a Category model which has_many Subcategories.

Another option has to do with Tags like this tutorial provides: https://www.sitepoint.com/tagging-scratch-rails/

Keep in mind that this is not a general classifieds site, it's specifically for musicians and musical instruments / services.

When using the site , you should be able to browse classifieds

  1. By searching
  2. By navigating from a navbar which will have the categories.

What is the best way to tackle this ?

  1. Should i have a lot of predifined tables in my db (subcategories) from which i auto fill forms on classified creation?
  2. Should i just implement Tags , and just save the forms (which gives the user more flexibility on data entry)?

Generally im kind of scratching my head about this and what would be the best way to do it.

Thanks in advance :)

2
  • You should focus on a single issue in your question. As is this question is asking far too many things and is very open ended and will most likly be closed – papirtiger Aug 28 '16 at 20:28
  • hmm i thought this stack was for more abstract subjects and talks, the question is around 2 concepts anyway :S – frcake Aug 29 '16 at 12:26
2

To build any sort of hierarchy you will want what is called a self joining relation. Lets consider a classic blogging application:

Diagram of a self joining table

class Category < ActiveRecord::Base
  has_many :subcategories, class_name: 'Category', 
                           foreign_key: 'parent_id'
  belongs_to :parent, class_name: 'Category'
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :category
end

This is an extremely powerful pattern as it lets you build any depth of hierarchy and lets you avoid taking multiple trips to the DB to query different tables.

In this case the relation between Post and Category is one to one. If we instead want a more natural many to many (M2M) relationship we need a join table:

M2M

class Category < ActiveRecord::Base
  has_many :subcategories, class_name: 'Category', 
                           foreign_key: 'parent_id'
  belongs_to :parent, class_name: 'Category'
  has_many :categorizations
  has_many :categories, through: :categorizations
end


class Categorization < ActiveRecord::Base
  belongs_to :category
  belongs_to :post
end

class Post < ActiveRecord::Base
  has_many :categorizations
  has_many :categories, through: :categorizations
end

This means for example that could a post could be categorized under recipes, baking, cookies, hobbies and gluten-free.

Each linking is done with a row in the categorizations table. No matter if you decide to call your taxonomies categories or tags the underlying building blocks are pretty much the same.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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