jeremy.bise-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jul-14  18:15 UTC
Rails Noob, Math or Array Issue?
Let''s say that below is the math for figuring out a unit in a popular
diet program:
    @fiber = Array.new(@food.fiber, 4)
    @points = (@food.calories / 50) + (@food.fat / 12) - (@fiber.min /
5)
And that for personal use, I''m creating a system to keep track of
these units on a daily basis and I''m simply rendering @points in the
following manner in my view:
   <p><b>Points:</b> <%= @points %></p>
But when @points equals 0, I get the nasty message:
 NoMethodError in FoodsController#show
You have a nil object when you didn''t expect it!
The error occurred while evaluating nil./
What would one do to make it OK to have a 0 instead of causing it to
evaluate nil?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
jeremy.bise-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jul-14  18:18 UTC
Re: Rails Noob, Math or Array Issue?
I goofed up...when the minimum of @fiber = 0, I get the nasty message...not when @points = 0. I apologize. On Jul 14, 2:15 pm, "jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Let''s say that below is the math for figuring out a unit in a popular > diet program: > > @fiber = Array.new(@food.fiber, 4) > @points = (@food.calories / 50) + (@food.fat / 12) - (@fiber.min / > 5) > > And that for personal use, I''m creating a system to keep track of > these units on a daily basis and I''m simply rendering @points in the > following manner in my view: > > <p><b>Points:</b> <%= @points %></p> > > But when @points equals 0, I get the nasty message: > > NoMethodError in FoodsController#show > > You have a nil object when you didn''t expect it! > The error occurred while evaluating nil./ > > What would one do to make it OK to have a 0 instead of causing it to > evaluate nil?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
jeremy.bise-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jul-14  19:16 UTC
Re: Rails Noob, Math or Array Issue?
Finally figured it out on my own with some modifications...
Controller:
  def show
    @food = Food.find(params[:id])
    @fiber = Array.new(@food.fiber.to_f, 4)
    @points = (@food.calories.to_f / 50) + (@food.fat.to_f / 12) -
((@fiber.min.to_f) / 5)
  end
View:
<p><b>Points:</b> <%= @points.round %></p>
Seems to produce the results I want...any better way would be welcome.
On Jul 14, 2:18 pm,
"jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I goofed up...when the minimum of @fiber = 0, I get the nasty
> message...not when @points = 0.  I apologize.
>
> On Jul 14, 2:15 pm,
"jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> wrote:
>
> > Let''s say that below is the math for figuring out a unit in a
popular
> > diet program:
>
> >     @fiber = Array.new(@food.fiber, 4)
> >     @points = (@food.calories / 50) + (@food.fat / 12) - (@fiber.min /
> > 5)
>
> > And that for personal use, I''m creating a system to keep
track of
> > these units on a daily basis and I''m simply rendering @points
in the
> > following manner in my view:
>
> >    <p><b>Points:</b> <%= @points %></p>
>
> > But when @points equals 0, I get the nasty message:
>
> >  NoMethodError in FoodsController#show
>
> > You have a nil object when you didn''t expect it!
> > The error occurred while evaluating nil./
>
> > What would one do to make it OK to have a 0 instead of causing it to
> > evaluate nil?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
hmm,
I don''t understand you Array.new statement
Array.new(x, 4) just returns an array with x lots of 4
Array.new(7, 4)  = [4,4,4,4,4,4,4]
Array.new(7.3,4) = [4,4,4,4,4,4,4]
@fiber = Array.new(x, 4)
@fiber.min = 4
whatever the x is.
I''d put all the code in the model
class Food
    def points
        value = 0
        value += (calories.to_f / 50) if calories
        value += (     fat.to_f / 12) if fat
        value += (   fiber.to_f /  5) if fiber
        return value
    end
end
and just call @food.points in your view
the problem you were having at the start was because one of these 
attributes (calories, fat or fiber)
was nil for the model,
I suggest setting :default => 0 in the migration.
Then you can get rid of the "if"s in the code above
jeremy.bise-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
wrote:> Finally figured it out on my own with some modifications...
> 
> Controller:
>   def show
>     @food = Food.find(params[:id])
>     @fiber = Array.new(@food.fiber.to_f, 4)
>     @points = (@food.calories.to_f / 50) + (@food.fat.to_f / 12) -
> ((@fiber.min.to_f) / 5)
>   end
> 
> View:
> <p><b>Points:</b> <%= @points.round %></p>
> 
> Seems to produce the results I want...any better way would be welcome.
> 
> On Jul 14, 2:18 pm,
"jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"
<jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
-- 
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
-~----------~----~----~----~------~----~------~--~---
jeremy.bise-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jul-17  13:48 UTC
Re: Rails Noob, Math or Array Issue?
What I need is something that returns the minimum of food.fiber or 4, whichever is lower. I appreciate your help--I''m just a programming idiot to begin with and even moreso at Ruby/Rails. On Jul 14, 3:29 pm, Matthew Rudy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> hmm, > I don''t understand you Array.new statement > > Array.new(x, 4) just returns an array with x lots of 4 > Array.new(7, 4) = [4,4,4,4,4,4,4] > Array.new(7.3,4) = [4,4,4,4,4,4,4] > > @fiber = Array.new(x, 4) > @fiber.min = 4 > > whatever the x is. > > I''d put all the code in the model > > class Food > def points > value = 0 > value += (calories.to_f / 50) if calories > value += ( fat.to_f / 12) if fat > value += ( fiber.to_f / 5) if fiber > return value > end > end > > and just call @food.points in your view > > the problem you were having at the start was because one of these > attributes (calories, fat or fiber) > was nil for the model, > I suggest setting :default => 0 in the migration. > Then you can get rid of the "if"s in the code above > > > > jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > Finally figured it out on my own with some modifications... > > > Controller: > > def show > > @food = Food.find(params[:id]) > > @fiber = Array.new(@food.fiber.to_f, 4) > > @points = (@food.calories.to_f / 50) + (@food.fat.to_f / 12) - > > ((@fiber.min.to_f) / 5) > > end > > > View: > > <p><b>Points:</b> <%= @points.round %></p> > > > Seems to produce the results I want...any better way would be welcome. > > > On Jul 14, 2:18 pm, "jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <jeremy.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > -- > 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 -~----------~----~----~----~------~----~------~--~---