Calling a model from a helper

I recently saw some Rails code where a model was called from a helper function. I was wondering if that is allowed by MVC?

Right now I'm working on this:

- my controller finds a list of projects...

@projects = Project.find(:all, :order => "name ASC")

- passes @projects to the view which passes it to a partial as a collection...

render(:partial => "project_row", :collection => @projects)

- the partial render a <tr> containing this... <tr>    <% @day.upto(@day+13) do |d| %>     <%= %{<td id="project_#{}_day_#{d.strftime "%m_%d_ %y"}">   <div>DES: {NO OF 'DES' HERE}</div>   <div>DEV: {NO OF 'DEV' HERE}</div>   <div>PMG: {NO OF 'PMG' HERE}</div>     </td>} %>   <% end %> </tr>

- ...I'm tempted to find the no. of DES/DEV/PMG by calling a helper function which will call the model and query the table for one entry on the right date, for the right project and for the right category (DES, DEV or PMG).

It would work but I'm not sure if this is how it is supposed to be done under MVC. Maybe someone can give me a hint as to the best way to do the above in Rails / MVC?



Thanks for the feedback Jamal.

Please note that you can access the models from both helpers and views. I just tested, by doing:

  def test     Project.find(:first).id   end

in my helper.

I was then able to display the project id in my view by calling the test method.

So it definitely can be done. The question is...should it be done? And if not, what's the MVC friendly way of doing this?



Hard and fast rules like never accessing a model directly in a helper are more harmful than helpful. You need to gain experience to figure out what makes the most sense. For instance, I have many helpers that call on a model to generate a select menu. Calling the model in the controller is confusing and error-prone in that kind of case because it really is view-related logic. Each action shouldn't have to know whether this select menu is even available. Keeping it in the helper is DRYest.

However in your case Jamal is right. The extra data you are querying for is related to projects. Therefore you should define methods on your Project class that return the data you need. You also might be able to set up joins and whatnot so this data is prefetched for all projects and you don't end up with 1+n queries.