So i am trying to check the return value of a query in my controller, and set a color for use in the view based on that. Here is the code snippet. @netwinnings = Event.find_by_sql ["select (sum(cashout) - sum (buyin)) as balance from events where user_id = ? ", @session [:user].id ] if @netwinnings.first.balance < 0 @netcolor = "green" else @netcolor = "red" end This would set @netcolor for me to use in the view, however i am constantly getting an error: undefined method `>'' for false:FalseClass /app/controllers/events_controller.rb:15:in `<'' /app/controllers/events_controller.rb:15:in `list'' any ideas ? Line 15 is the (if @netwinnings) line...I know this value is ok, since i print it in the view without problems, however this if/else block breaks on me. thanks adam _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi,> > @netwinnings = Event.find_by_sql ["select (sum(cashout) - sum > (buyin)) as balance from events where user_id = ? ", @session > [:user].id ] >Sure that this return something ? And that`s the correct type ? I`m new at Rails but I remember the same problem with a wrong type.> > if @netwinnings.first.balance < 0 > @netcolor = "green" > else > @netcolor = "red" > end > > This would set @netcolor for me to use in the view, however i am > constantly getting an error: > > undefined method `>'' for false:FalseClass > > /app/controllers/events_controller.rb:15:in `<'' > /app/controllers/events_controller.rb:15:in `list'' > > > > any ideas ? Line 15 is the (if @netwinnings) line...I know this > value is ok, since i print it in the view without problems, however > this if/else block breaks on me.cu odo _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
yes positive b/c i print out @netwinnings.first.balance in the view without problems. On Aug 22, 2005, at 10:39 AM, Oliver Dohmen wrote:> Hi, > >> >> @netwinnings = Event.find_by_sql ["select (sum(cashout) - sum >> (buyin)) as balance from events where user_id = ? ", @session >> [:user].id ] >> > Sure that this return something ? And that`s the correct type ? I`m > new at Rails but I remember the same problem with a wrong type. >> >> if @netwinnings.first.balance < 0 >> @netcolor = "green" >> else >> @netcolor = "red" >> end >> >> This would set @netcolor for me to use in the view, however i am >> constantly getting an error: >> >> undefined method `>'' for false:FalseClass >> >> /app/controllers/events_controller.rb:15:in `<'' >> /app/controllers/events_controller.rb:15:in `list'' >> >> >> >> any ideas ? Line 15 is the (if @netwinnings) line...I know this >> value is ok, since i print it in the view without problems, >> however this if/else block breaks on me. > > cu odo > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
You''re probably getting a string back. Proof: H:\>irb irb(main):001:0> "15" < 0 NoMethodError: undefined method `>'' for false:FalseClass from (irb):1:in `<'' from (irb):1 irb(main):002:0> Try something like this: irb(main):002:0> "15".to_i < 0 => false irb(main):004:0> "15".to_f < 0 => false On 8/22/05, Adam Denenberg <adam-fpx97dFL/ODYtjvyW6yDsg@public.gmane.org> wrote:> yes positive b/c i print out @netwinnings.first.balance in the view without > problems. > > > > On Aug 22, 2005, at 10:39 AM, Oliver Dohmen wrote: > > Hi, > > > > @netwinnings = Event.find_by_sql ["select (sum(cashout) - sum(buyin)) as > balance from events where user_id = ? ", @session[:user].id ] > > Sure that this return something ? And that`s the correct type ? I`m new at > Rails but I remember the same problem with a wrong type. > > > if @netwinnings.first.balance < 0 > @netcolor = "green" > else > @netcolor = "red" > end > > This would set @netcolor for me to use in the view, however i am constantly > getting an error: > > undefined method `>'' for false:FalseClass > > /app/controllers/events_controller.rb:15:in `<'' > /app/controllers/events_controller.rb:15:in `list'' > > > > any ideas ? Line 15 is the (if @netwinnings) line...I know this value is > ok, since i print it in the view without problems, however this if/else > block breaks on me. > > cu odo > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Brock Weaver [OBC]Technique
Hi, Am 22.08.2005 um 16:46 schrieb Adam Denenberg:> yes positive b/c i print out @netwinnings.first.balance in the view > without problems. >Ok. Tried @netwinnings.first.balance.to_i < 0 cu odo P.S. I`m a Ruby Newbie to so don`t expect too much, but i will try _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
this worked thanks. Will ruby always get a value back as a string, even if the DB columns are defined as floats ? For others, final code is : if @netwinnings.first.balance.to_i < 0 @netcolor = "red" else @netcolor = "green" end On Aug 22, 2005, at 10:55 AM, Brock Weaver wrote:> You''re probably getting a string back. Proof: > > H:\>irb > irb(main):001:0> "15" < 0 > NoMethodError: undefined method `>'' for false:FalseClass > from (irb):1:in `<'' > from (irb):1 > irb(main):002:0> > > Try something like this: > irb(main):002:0> "15".to_i < 0 > => false > irb(main):004:0> "15".to_f < 0 > => false > > On 8/22/05, Adam Denenberg <adam-fpx97dFL/ODYtjvyW6yDsg@public.gmane.org> wrote: > >> yes positive b/c i print out @netwinnings.first.balance in the >> view without >> problems. >> >> >> >> On Aug 22, 2005, at 10:39 AM, Oliver Dohmen wrote: >> >> Hi, >> >> >> >> @netwinnings = Event.find_by_sql ["select (sum(cashout) - sum >> (buyin)) as >> balance from events where user_id = ? ", @session[:user].id ] >> >> Sure that this return something ? And that`s the correct type ? >> I`m new at >> Rails but I remember the same problem with a wrong type. >> >> >> if @netwinnings.first.balance < 0 >> @netcolor = "green" >> else >> @netcolor = "red" >> end >> >> This would set @netcolor for me to use in the view, however i am >> constantly >> getting an error: >> >> undefined method `>'' for false:FalseClass >> >> /app/controllers/events_controller.rb:15:in `<'' >> /app/controllers/events_controller.rb:15:in `list'' >> >> >> >> any ideas ? Line 15 is the (if @netwinnings) line...I know this >> value is >> ok, since i print it in the view without problems, however this if/ >> else >> block breaks on me. >> >> cu odo >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> >> > > > -- > Brock Weaver > [OBC]Technique > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
I believe it types things correctly on fields defined in the schema. But in your example, you''re calculating the value -- it probably just kicks it back as a string in this case. I don''t know if that''s an ActiveRecord thing or the underlying driver, so you''ll just have to be aware of these types of problems. Whenever you deal with problems in data from a db, first assume either incorrect typing or badly coerced data. That''s the problem 9/10 times. BTW, using the interactive ruby interpretter (irb) is very handy for things like this. Also, writing a script which lets you pass SQL to it and kick out the result is nice to have around too, so you can call it via irb. (just use the load statement, like "load sqltest.rb<enter>select "select * from users"<enter>) On 8/22/05, Adam Denenberg <adam-fpx97dFL/ODYtjvyW6yDsg@public.gmane.org> wrote:> this worked thanks. Will ruby always get a value back as a string, > even if the DB columns are defined as floats ? > > For others, final code is : > > if @netwinnings.first.balance.to_i < 0 > @netcolor = "red" > else > @netcolor = "green" > end > > > > On Aug 22, 2005, at 10:55 AM, Brock Weaver wrote: > > > You''re probably getting a string back. Proof: > > > > H:\>irb > > irb(main):001:0> "15" < 0 > > NoMethodError: undefined method `>'' for false:FalseClass > > from (irb):1:in `<'' > > from (irb):1 > > irb(main):002:0> > > > > Try something like this: > > irb(main):002:0> "15".to_i < 0 > > => false > > irb(main):004:0> "15".to_f < 0 > > => false > > > > On 8/22/05, Adam Denenberg <adam-fpx97dFL/ODYtjvyW6yDsg@public.gmane.org> wrote: > > > >> yes positive b/c i print out @netwinnings.first.balance in the > >> view without > >> problems. > >> > >> > >> > >> On Aug 22, 2005, at 10:39 AM, Oliver Dohmen wrote: > >> > >> Hi, > >> > >> > >> > >> @netwinnings = Event.find_by_sql ["select (sum(cashout) - sum > >> (buyin)) as > >> balance from events where user_id = ? ", @session[:user].id ] > >> > >> Sure that this return something ? And that`s the correct type ? > >> I`m new at > >> Rails but I remember the same problem with a wrong type. > >> > >> > >> if @netwinnings.first.balance < 0 > >> @netcolor = "green" > >> else > >> @netcolor = "red" > >> end > >> > >> This would set @netcolor for me to use in the view, however i am > >> constantly > >> getting an error: > >> > >> undefined method `>'' for false:FalseClass > >> > >> /app/controllers/events_controller.rb:15:in `<'' > >> /app/controllers/events_controller.rb:15:in `list'' > >> > >> > >> > >> any ideas ? Line 15 is the (if @netwinnings) line...I know this > >> value is > >> ok, since i print it in the view without problems, however this if/ > >> else > >> block breaks on me. > >> > >> cu odo > >> > >> _______________________________________________ > >> Rails mailing list > >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > >> > >> _______________________________________________ > >> Rails mailing list > >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > >> > >> > >> > >> > > > > > > -- > > Brock Weaver > > [OBC]Technique > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Brock Weaver [OBC]Technique
On 22.8.2005, at 18.04, Adam Denenberg wrote:> this worked thanks. Will ruby always get a value back as a string, > even if the DB columns are defined as floats ?This is up to the db adapters. Normally the adapters can cast the value to a correct type according to the table definition. However, you have a custom sql clause there so I doubt that the adapters can sniff the type of the balance in this case. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 22.8.2005, at 18.04, Adam Denenberg wrote:> this worked thanks. Will ruby always get a value back as a string, > even if the DB columns are defined as floats ?This is up to the db adapters. Normally the adapters can cast the value to a correct type according to the table definition. However, you have a custom sql clause there so I doubt that the adapters can sniff the type of the balance in this case. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails