Calling a model from a helper


#1

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”)

  • …it passes @projects to the view which passes it to a partial as a
    collection…

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

  • the partial render a containing this… <% @day.upto(@day+13) do |d| %> <%= %{
    DES: {NO OF 'DES' HERE}
    DEV: {NO OF 'DEV' HERE}
    PMG: {NO OF 'PMG' HERE}
    } %> <% end %>
    • …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,

    Gabor


#2

gabordemeter wrote:

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”)

Hi,

You cannot access the models from the helper, so this is not allowed.

I don’t see any problem with the way you are doing it :slight_smile:

Regards,
Jamal


#3

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?

Best,

Gabor

On Jun 1, 1:05 pm, Jamal S. removed_email_address@domain.invalid


#4

gabordemeter wrote:

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?

Best,

Gabor

On Jun 1, 1:05 pm, Jamal S. removed_email_address@domain.invalid

My mistake sorry, I was testing if I could receive ids from User model
which did not exist before.

Anyway, you should not let the helper connect to your models, this
should your controller do for you. This is how things work with MVC.

Helper are made to cut things down for things in your views.

For example you could make a method called h1, so you don’t need to
create both tags

def h1(text)

” + text + "


end

#5

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.

On Jun 1, 3:27 pm, Jamal S. removed_email_address@domain.invalid