Hi- What I would like to do in the controller is select 2 rows from my DB and manipulate those rows to be rendered in the view. result = MyModel.find(:all, :limit => 2, :select => column) # Gives me 2 rows. @view_stuff = result[0] - result[1] #Get error for not having method ''-'' Do I need to write a class method to manipulate the result set? Even if the column type is of type float? Is there a better approach here? Thanks! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
should be result[0].column - result[1].column i think -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
That''s what I thought it would be too, then I get the following: "undefined method `column'' for -2268.0:Float" Any ideas? On Jan 29, 2:23 pm, Thorsten Mueller <rails-mailing-l...@andreas- s.net> wrote:> should be > result[0].column - result[1].column > i think > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
pete wrote:> That''s what I thought it would be too, then I get the following: > > "undefined method `column'' for -2268.0:Float" > > Any ideas? > > On Jan 29, 2:23 pm, Thorsten Mueller <rails-mailing-l...@andreas-just checked it like that: foo=ShipmentDivisions.find(:all, :limit => 2, :select => :amount) where ShipmentDivisions has a col "amount" foo[0].amount - foo[1].amount works fine your result = MyModel.find(:all, :limit => 2, :select => column) uses (at least as typed here) a var column which must be a string with a col name this you can''t use like result[0].column you would have to use something like result[0][column] if you need variable col names here. otherwise just use result[0].the_absolute_colname -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
AMAZING! That worked!! Why do you have to do "result[0][column]" when column is a variable? THANK YOU!! On Jan 29, 2:49 pm, Thorsten Mueller <rails-mailing-l...@andreas- s.net> wrote:> pete wrote: > > That''s what I thought it would be too, then I get the following: > > > "undefined method `column'' for -2268.0:Float" > > > Any ideas? > > > On Jan 29, 2:23 pm, Thorsten Mueller <rails-mailing-l...@andreas- > > just checked it like that: > foo=ShipmentDivisions.find(:all, :limit => 2, :select => :amount) > > where ShipmentDivisions has a col "amount" > foo[0].amount - foo[1].amount works fine > > your > result = MyModel.find(:all, :limit => 2, :select => column) > > uses (at least as typed here) a var column which must be a string with a > col name > this you can''t use like > result[0].column > > you would have to use something like > result[0][column] > > if you need variable col names here. otherwise just use > result[0].the_absolute_colname > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> > Why do you have to do "result[0][column]" when column is a variable? >easy enough: result[0] is an object and result[0].xxx calls a function of this object all columns are defined as functions for ActiveRecord objects but your column is a string, containing the column name. there is no function column, since there is no column with that name ActiveRecord::[] is just an alternative way to request the content of a col in a slightly more flexible way, by handing over the name as a string -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Perfect, thanks again for your help with this! On Jan 29, 3:06 pm, Thorsten Mueller <rails-mailing-l...@andreas- s.net> wrote:> > Why do you have to do "result[0][column]" when column is a variable? > > easy enough: > result[0] is an object and result[0].xxx calls a function of this object > all columns are defined as functions for ActiveRecord objects > but your column is a string, containing the column name. there is no > function column, since there is no column with that name > ActiveRecord::[] is just an alternative way to request the content of a > col in a slightly more flexible way, by handing over the name as a > string > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---