Rails queries or methods?

I'm building a Rails application and want to know which one is better:

  • Store data by default, such as category names, in the database and receive data with queries;

  • or create a method in ApplicationController that with category_id

    as argument returns information about the desired category?

Obs: Values ​​will rarely be changed.

Thank you Gabriel.

+2


source to share


2 answers


I always think so:

  • how many items are there? If more than 3 I would go with db
  • Is this app for me or a client? If it's for a client, I would go with a db (a little crud interace in admin so that he can add / remove / modify the categories themselves even after development is complete).


Things like categories are data that comes from customers - if they come from customers, customers have control over what's inside (and save time to change the source code). This way they can provide an excel / cvs file that you can use to load the db with the sources (and without modifying the source). It's also easier to later do various tricks in SQL (join) if the categories are in the db.

IMHO - the only place I would use hardcoded hash / array is some (2-3) multiple value attributes like custom roles (assuming they are static of course).

+2


source


A common pattern could be to store data in a database table (in this case, create a table categories

) and just use the ActiveRecord associations as usual.



You can have seed data to maintain values ​​for categories. There were a lot of plugins that dealt with the seed data problem, but now Rails has a rake command that runs the code in db / seed.rb. This railscast has details.

+1


source







All Articles