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

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!

Hey,

comments below:

On 30-Mar-07, at 9:21 PM, [email protected] 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

Trevor,

This is AWESOME! Thanks a lot for the help. :slight_smile:

—A