# Moving logic to controller in for loop - newbie question

**URL:** <https://www.ruby-forum.com/t/moving-logic-to-controller-in-for-loop-newbie-question/115216>\
**Category:** Rails\
**Created:** [September 21, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/moving-logic-to-controller-in-for-loop-newbie-question/115216 "2007-09-21T20:25:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rails-nooby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rails-nooby](https://www.ruby-forum.com/u/rails-nooby)\
**Post date:** [September 21, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/moving-logic-to-controller-in-for-loop-newbie-question/115216/1 "2007-09-21T20:25:13Z")

</div>

Hi,

Im new at this. I have something like this in my list view

\<% for property in @properties %\>  
…

\<%= number\_to\_currency((property.current\_value -  
property.mortgage\_amount), {:unit =\> “£”}) %\>  
\<% end %\>

which has the desired effect.

However I understand I should be putting this sort of logic into a  
controller.

Ive tried this in my controller

@get\_property\_equity = Property.find(:id).current\_value -  
Property.find(:id).mortgage\_amoount

This works(sort of) if I enter an id manually but I am not sure how to  
pass the id in my loop in my view, indeed if thats what I should be  
doing?

Any help greatly appreciated

g

---

<div class="post-metadata">

**Author:** ![rails-nooby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rails-nooby](https://www.ruby-forum.com/u/rails-nooby)\
**Post date:** [September 21, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/moving-logic-to-controller-in-for-loop-newbie-question/115216/2 "2007-09-21T20:47:42Z")

</div>

Mike P. wrote:

snip

> Greg, it seems like this is something better suited as a model  
> attribute. Put this on your Property model:
> 
> def current\_equity  
> self.current\_value - self.mortgage\_amount  
> end
> 
> Then in your view, you can just do this:
> 
> number\_to\_currency(property.current\_equity, {:unit =\> “£”})
> 
> Try that and see what happens.
> 
> mike

Thanks so much Mike.

Works perfectly.

---

<div class="post-metadata">

**Author:** ![rails-nooby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@rails-nooby](https://www.ruby-forum.com/u/rails-nooby)\
**Post date:** [September 21, 2007, 8:43pm UTC](https://www.ruby-forum.com/t/moving-logic-to-controller-in-for-loop-newbie-question/115216/3 "2007-09-21T20:43:49Z")

</div>

Greg Plumbly wrote:

> Hi,
> 
> Im new at this. I have something like this in my list view
> 
> \<% for property in @properties %\>  
> …
> 
> \<%= number\_to\_currency((property.current\_value -  
> property.mortgage\_amount), {:unit =\> “£”}) %\>  
> \<% end %\>
> 
> which has the desired effect.
> 
> However I understand I should be putting this sort of logic into a  
> controller.
> 
> Ive tried this in my controller
> 
> @get\_property\_equity = Property.find(:id).current\_value -  
> Property.find(:id).mortgage\_amoount
> 
> This works(sort of) if I enter an id manually but I am not sure how to  
> pass the id in my loop in my view, indeed if thats what I should be  
> doing?
> 
> Any help greatly appreciated
> 
> g

Greg, it seems like this is something better suited as a model  
attribute. Put this on your Property model:

def current\_equity  
self.current\_value - self.mortgage\_amount  
end

Then in your view, you can just do this:

number\_to\_currency(property.current\_equity, {:unit =\> “£”})

Try that and see what happens.

mike
