I’m looking to load a single (chosen randomly) object from a single table in my database on every page of my rails app.
For example, a quotes table which has several quotes in the table, and I just want one on every page load.
What’s the best way to accomplish this? Obviously copy & pasting the query into each controller isn’t the right way to go about this.
I wouldn’t use
before_filterfor this, there is no need to access database on redirecting actions and other “not rendered” actions. Instead I would usehelper_function, and I would call it in the layout, as you need to position it anyways.Your method of selecting a quote is up to you. You just need to access
random_quoteas a normal helper method in the layout. This only access one quote per action, only if the layout is rendered.