My Ruby on Rails must generate reports based on database records. I stored the SQL and ERB code needed to create these reports in the database. Each report consists of three parts:
- Header representing an ERB fragment displayed with
<%= render inline: @report.header %> - The footer, which is another fragment of the ERB, using
<%= render inline: @report.footer %>
Then the body from which the content is evaluated using @rows = eval("#{@report.model}.find_by_sql('#{@report.query}')")is displayed using:
<% @rows.each do |row| %>
<tr>
<% row.attributes.each_value do |value| %>
<td><%= value %></td>
<% end %>
</tr>
<% end %>
Model in @report.modelis the name of the Ruby class stored in the column.
While this makes the job perfectly fine, I began to feel uncomfortable storing the actual source code in the database. An alternative that was suggested instead of storing code in a database would be to save the code in and from files related to these files. It doesn't sound much better to me (actually it’s more of a hassle, because instead of just displaying the text in the database column, I need to open and read the file).
What is the general consensus for storing source code like this in a database, and what are some (better) alternatives: Some considerations include:
- New data is rarely added (we are talking about reports based on federal rules that have not changed since 1999).
- , , ( , , , , ).
- . .