# Calling a model from a helper

**URL:** <https://www.ruby-forum.com/t/calling-a-model-from-a-helper/105733>\
**Category:** Rails\
**Created:** [June 1, 2007, 9:40pm UTC](https://www.ruby-forum.com/t/calling-a-model-from-a-helper/105733 "2007-06-01T21:40:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabordemeter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9de053/32.png) [@gabordemeter](https://www.ruby-forum.com/u/gabordemeter)\
**Post date:** [June 1, 2007, 9:40pm UTC](https://www.ruby-forum.com/t/calling-a-model-from-a-helper/105733/1 "2007-06-01T21:40:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gabordemeter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9de053/32.png) [@gabordemeter](https://www.ruby-forum.com/u/gabordemeter)\
**Post date:** [June 1, 2007, 10:05pm UTC](https://www.ruby-forum.com/t/calling-a-model-from-a-helper/105733/2 "2007-06-01T22:05:01Z")

</div>

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 🙂

Regards,  
Jamal

---

<div class="post-metadata">

**Author:** ![gabordemeter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9de053/32.png) [@gabordemeter](https://www.ruby-forum.com/u/gabordemeter)\
**Post date:** [June 1, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/calling-a-model-from-a-helper/105733/3 "2007-06-01T22:40:12Z")

</div>

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](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![gabordemeter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9de053/32.png) [@gabordemeter](https://www.ruby-forum.com/u/gabordemeter)\
**Post date:** [June 1, 2007, 11:27pm UTC](https://www.ruby-forum.com/t/calling-a-model-from-a-helper/105733/4 "2007-06-01T23:27:29Z")

</div>

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](mailto: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

---

<div class="post-metadata">

**Author:** ![gabordemeter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9de053/32.png) [@gabordemeter](https://www.ruby-forum.com/u/gabordemeter)\
**Post date:** [June 2, 2007, 3:31am UTC](https://www.ruby-forum.com/t/calling-a-model-from-a-helper/105733/5 "2007-06-02T03:31:39Z")

</div>

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](mailto:removed_email_address@domain.invalid)
