Caching custom words on view

Hello guys,

I’ve never used Rails cache before but I think I just found where to use it.

I have an application with some dashboards. User may want to customize some of the words that are displayed on the dashboard, e.g., Average sales (default) may become Average rents or Average views. Since this may appear in many places in the app, I’m thinking the following approach:

  • Create a CustomWord model with the attributes key:symbol, word:string and organization_id:integer.

  • Create a helper method custom(:key) that receives the key and uses the current_user.organization_id to get the word.

This would already solve my problem, but I believe it would create some performance issues, since it would touch the database for each of those words.

I believe model cache is the solution, right? Is this the best approach? How can I do this?


I created that structure in It’s just a proof of concept. Check Post#index view.

Hi @jdanielnd, have you considered creating a Dashboard model? It seems that the correct level of abstraction for that.

If you are using Postgres, for instance, you could use a “fields” attribute with a data type of hstore or json to store the custom keys for each user.

I don’t know how big your application is, but I’d also recommend not worrying about performance at an initial stage but instead focusing on getting abstractions and design right.

In the long run, they will make it easy for you to optimise and improve performance. I’ve seen plenty of apps that kick off worrying about performance and find themselves constrained to the point of having no options in the future.

I tend to think of the initial design with a mindset of “cost free” performance wise, and it does help a lot.