andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-31 04:21 UTC
Help moving code into model... get row, modify contents, return to controller
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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Trevor Squires
2007-Mar-31 05:37 UTC
Re: Help moving code into model... get row, modify contents, return to controller
Hey, comments below: On 30-Mar-07, at 9:21 PM, andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > 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 >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" > endSame 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> Thanks in advance! > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-31 13:06 UTC
Re: Help moving code into model... get row, modify contents, return to controller
Trevor, This is AWESOME! Thanks a lot for the help. :) ---A On Mar 31, 1:37 am, Trevor Squires <tre...-k8q5a0yEZAgS+FvcfC7Uqw@public.gmane.org> wrote:> Hey, > > comments below: > > On 30-Mar-07, at 9:21 PM, andrew.ohns...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > > > > > > 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 > > 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 > > > Thanks in advance!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---