# Help moving code into model... get row, modify contents, ret

**URL:** <https://www.ruby-forum.com/t/help-moving-code-into-model-get-row-modify-contents-ret/90703>\
**Category:** Rails\
**Created:** [March 31, 2007, 6:22am UTC](https://www.ruby-forum.com/t/help-moving-code-into-model-get-row-modify-contents-ret/90703 "2007-03-31T06:22:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 31, 2007, 6:22am UTC](https://www.ruby-forum.com/t/help-moving-code-into-model-get-row-modify-contents-ret/90703/1 "2007-03-31T06:22:38Z")

</div>

This is probably a total noob question, but I can’t figure it out from  
any of my available resources…

I’m trying to move some code into the model. I want to fetch a row  
from the database, modify some of the contents, return the modified  
version to the controller, WITHOUT saving the changes to the DB. The  
reason behind this is that I have some status fields in the DB that  
equate to words… rather than save strings in the DB, I figured I  
would make the columns tinyints, and then change “1” into “Order  
pending processing,” “2” into “Order entered production,” “3” into  
“Production complete, awaiting QC,” and so on.

So in my controller I have:

@order = Order.get\_formatted\_order(params[:id)

and in my model I have:

def self.get\_formatted\_order(id)  
@order = Order.find(id)  
end

This works fine and returns the array to the controller, duh. But  
whenever I try to tinker with the contents, wierd things happen.  
First I tried to just replace one element in the array for fun:

def self.get\_formatted\_order(id)  
@order = Order.find(id)  
@order.status = “In Production”  
end

And the odd thing to me is that this only returns “In Production” not  
the whole array. So then I figured I would get smart…

def self.get\_formatted\_order(id)  
@order = Order.find(id)  
self.status = “In Production”  
end

This gets me a “undefined method `status`…” In desparation I tried  
self[:status] and @order[:status] and get variations of the same two  
problems.

So how can I pull a row into an array, muss with it, and return the  
whole thing to the controller without saving it back to the DB?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 31, 2007, 7:40am UTC](https://www.ruby-forum.com/t/help-moving-code-into-model-get-row-modify-contents-ret/90703/2 "2007-03-31T07:40:42Z")

</div>

Hey,

comments below:

On 30-Mar-07, at 9:21 PM, removed\_email\_address@domain.invalid wrote:

> pending processing," “2” into “Order entered production,” “3” into  
> end
> 
> This works fine and returns the array to the controller, duh. But  
> whenever I try to tinker with the contents, wierd things happen.  
> First I tried to just replace one element in the array for fun:
> 
> def self.get\_formatted\_order(id)  
> @order = Order.find(id)  
> @order.status = “In Production”  
> end

That’s because a ruby method returns the result of your last  
statement - the result there is the value you just assigned to  
@order.status

> And the odd thing to me is that this only returns “In Production” not  
> the whole array. So then I figured I would get smart…
> 
> def self.get\_formatted\_order(id)  
> @order = Order.find(id)  
> self.status = “In Production”  
> end

Same deal - it returns whatever you just assigned to self.status

> This gets me a “undefined method `status`…” In desparation I tried  
> self[:status] and @order[:status] and get variations of the same two  
> problems.
> 
> So how can I pull a row into an array, muss with it, and return the  
> whole thing to the controller without saving it back to the DB?

So, the answer to your question is to finish your method with just  
“@order” on it’s own line (or if it makes it clearer for you, return  
@order - but that’s actually a tad slower).

However, I think you’re trying to be too clever by mangling your  
model data just to present human-readable representations of things  
like status fields. Just write accessors that do the translation:

class Order \< ActiveRecord::Base  
ORDER\_STATUS\_MAP = { 1 =\> “Pending”, 2 =\> “Processing”}

def status\_string  
ORDER\_STATUS\_MAP[self.status]  
end  
end

Then in your views you can do:

Order Status: \<%= order.status\_string %\>

And you don’t have to torture your Order class with stuff like  
“get\_formatted\_order”.

HTH,  
Trevor

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 31, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/help-moving-code-into-model-get-row-modify-contents-ret/90703/3 "2007-03-31T15:07:20Z")

</div>

Trevor,

This is AWESOME! Thanks a lot for the help. 🙂

—A
