views:

47

answers:

2

Hello.

I'm creating a Rails app and would like to know which of these is better:

  1. Store default data, like categories names, in a DB and get the data with queries;

  2. or create a method in ApplicationController that, with the category_id as argument, returns info about the desired category?

Obs: The values will rarely be changed.

Thank you, Gabriel.

+1  A: 

A common pattern may be to store the data in a database table (in this case, create a categories table) and just use ActiveRecord associations as you normally would.

You can have seed data to maintain the values for categories. There have been many plugins that address the seed data problem, but now Rails has a rake task that runs code in db/seed.rb. This railscast takes you through the details.

hgimenez
+2  A: 

I always think in this way:

  • how many elements are there? If more than 3 i would go with db
  • is it application for myself or a client? If it's for a client I would go with db (little crud interace in admin so he can add/remove/modify the categories himself even after the end of development).

Things like categories are the data that come from the customers - if they come from customers give the customers the control what is inside (and save yourself the time to change source code). Like this they can provide the excel/cvs file that you can use for seeding the db with initial records (and without changing the source code). It's also easier later to make different tricks in SQL (joins) if the categories are in db.

IMHO the only place where I would use the hardcoded hash/array is some (2-3) multi values attributes like user roles (of course if they are static).

j t