# Calling an actionview method from inside a model

**URL:** <https://www.ruby-forum.com/t/calling-an-actionview-method-from-inside-a-model/55004>\
**Category:** Rails\
**Created:** [March 16, 2006, 2:44am UTC](https://www.ruby-forum.com/t/calling-an-actionview-method-from-inside-a-model/55004 "2006-03-16T02:44:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [March 16, 2006, 2:44am UTC](https://www.ruby-forum.com/t/calling-an-actionview-method-from-inside-a-model/55004/1 "2006-03-16T02:44:47Z")

</div>

Hi,

I would like my model instance to produce it’s own list of options for  
a form select. This is from a product model that has\_many variations

def alts\_for\_select(current\_id)  
the\_map = variations.map{|v| [v.name, v.id]}  
options\_for\_select(the\_map, current\_id)  
end

the model cannot see the options\_for\_select method

I tried to use

ActionView::Helpers::FormOptionsHelper::options\_for\_select(the\_map,  
current\_id)

but this didn’t work.

Any ideas?

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [March 16, 2006, 2:56am UTC](https://www.ruby-forum.com/t/calling-an-actionview-method-from-inside-a-model/55004/2 "2006-03-16T02:56:49Z")

</div>

General consensus would say this is bad design for making your model  
tied to  
your view.

The better way to do this would be to put a method in a helper and call  
it  
from there if you really don’t want to put the code in your view.

-Nick

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [March 16, 2006, 3:24am UTC](https://www.ruby-forum.com/t/calling-an-actionview-method-from-inside-a-model/55004/3 "2006-03-16T03:24:06Z")

</div>

On 3/15/06, Nick S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> General consensus would say this is bad design for making your model tied to  
> your view.
> 
> The better way to do this would be to put a method in a helper and call it  
> from there if you really don’t want to put the code in your view.

OK, that sounds reasonable but here is more of my situation. I have an  
array of objects that I want to display in a view. Each object has to  
produce a drop down box. However, each object might be an instance of  
three different classes. Currently I have a if elsif elsif end  
structure that determines which class the object is from. Then the  
drop down box is created based on special rules for that drop down  
box. One class needs a drop down box that may or may not use grouped  
sections (option\_groups\_from\_collection\_for\_select). I was hoping to  
move the logic to the model and then I would only need this in my view  
(or something simliar)

\<% objs.each do |o| %\> \<%= o.alts\_for\_select(o.current\_id) %\> \<% end %\>

The elsif (or case) structure is gone which is kind of nice.

ideas?

Thanks,

Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [March 16, 2006, 3:48am UTC](https://www.ruby-forum.com/t/calling-an-actionview-method-from-inside-a-model/55004/4 "2006-03-16T03:48:31Z")

</div>

In Model

def alts\_for\_select  
variations.map{|v| [v.name, v.id]}  
end

In View

\<% objs.each do |o| %\>  
\<%= options\_for\_select(o.alts\_for\_select, o.current\_id) %\>  
\<% end %\>

Steven Beales

“Peter M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote  
in message  
news:removed\_email\_address@domain.invalid…  
On 3/15/06, Nick S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> General consensus would say this is bad design for making your model tied  
> to  
> your view.
> 
> The better way to do this would be to put a method in a helper and call it  
> from there if you really don’t want to put the code in your view.

OK, that sounds reasonable but here is more of my situation. I have an  
array of objects that I want to display in a view. Each object has to  
produce a drop down box. However, each object might be an instance of  
three different classes. Currently I have a if elsif elsif end  
structure that determines which class the object is from. Then the  
drop down box is created based on special rules for that drop down  
box. One class needs a drop down box that may or may not use grouped  
sections (option\_groups\_from\_collection\_for\_select). I was hoping to  
move the logic to the model and then I would only need this in my view  
(or something simliar)

\<% objs.each do |o| %\> \<%= o.alts\_for\_select(o.current\_id) %\> \<% end %\>

The elsif (or case) structure is gone which is kind of nice.

ideas?

Thanks,

Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [March 16, 2006, 3:54am UTC](https://www.ruby-forum.com/t/calling-an-actionview-method-from-inside-a-model/55004/5 "2006-03-16T03:54:38Z")

</div>

Hi Steven,

After Nick’s replay I did think about this but about if one of objs  
for one of the classes has to produce a grouped select? Argg. Maybe  
it’s messy no matter what.  
Thanks,

Peter
