I am passing hard time with ruby on rails ...I was trying to count the average of a whole table......at first I calculated avarage of eah column then average of the average values but it is giving me an error- here are my codes and error- error- NoMethodError in Rate_schools#index Showing */home/vmuser/workspace/project3 -3/app/views/rate_schools/index.html.erb* where line *#36* raised: undefined method `+'' for nil:NilClass Extracted source (around line *#36*): 33: 34: 35: 36: Average <%=((@avg1+@avg2+@avg3+@avg4+@avg5+@avg6+@avg7+@avg8)/8)%> 37: <% end %> 38: 39: </tr> codes In controller: def index @school=School.find(params[:id]) @rate_school =@school.rate_schools.build @avg1=@school.rate_schools.average(:library) @avg2=@school.rate_schools.average(:careercenter) @avg3=@school.rate_schools.average(:location) @avg4=@school.rate_schools.average(:food) @avg5=@school.rate_schools.average(:healthcenter) @avg6=@school.rate_schools.average(:studentactivities) @avg7=@school.rate_schools.average(:facilities) @avg8=@school.rate_schools.average(:internetspeed) codes in index : <% if !@school.rate_schools.blank? %> <td> library=<%=@avg1%> </td> <td>careercenter=<%=@avg2%> </td> <td>location=<%=@avg3%> </td> <td>Food=<%=@avg4%> </td> <td>Healthcenter=<%=@avg5%> </td> <td>studentactivities=<%=@avg6%> </td> <td>facilities=<%=@avg7%> </td> <td>internetspeed=<%=@avg8%> </td> Average <%=((@avg1+@avg2+@avg3+@avg4+@avg5+@avg6+@avg7+@avg8)/8)%> <% end %> </tr> </table> -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/wUYDoovBR1UJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
You should have a nil value to any @avg, try to use @avgN = @school.rate_schools.average(:X) || 0 2012/5/20 Roy <poddojaroy-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> I am passing hard time with ruby on rails ...I was trying to count the > average of a whole table......at first I calculated avarage of eah column > then average of the average values but it is giving me an error- here are > my codes and error- > error- > > NoMethodError in Rate_schools#index > > Showing */home/vmuser/workspace/project3 > -3/app/views/rate_schools/index.html.erb* where line *#36* raised: > > undefined method `+'' for nil:NilClass > > Extracted source (around line *#36*): > > 33: > 34: > 35: > 36: Average <%=((@avg1+@avg2+@avg3+@avg4+@avg5+@avg6+@avg7+@avg8)/8)%> > 37: <% end %> > 38: > 39: </tr> > > > > codes In controller: > def index > > @school=School.find(params[:id]) > @rate_school =@school.rate_schools.build > > > @avg1=@school.rate_schools.average(:library) > @avg2=@school.rate_schools.average(:careercenter) > @avg3=@school.rate_schools.average(:location) > @avg4=@school.rate_schools.average(:food) > @avg5=@school.rate_schools.average(:healthcenter) > @avg6=@school.rate_schools.average(:studentactivities) > @avg7=@school.rate_schools.average(:facilities) > @avg8=@school.rate_schools.average(:internetspeed) > > codes in index : > <% if !@school.rate_schools.blank? %> > > <td> library=<%=@avg1%> </td> > <td>careercenter=<%=@avg2%> </td> > <td>location=<%=@avg3%> </td> > <td>Food=<%=@avg4%> </td> > <td>Healthcenter=<%=@avg5%> </td> > <td>studentactivities=<%=@avg6%> </td> > <td>facilities=<%=@avg7%> </td> > <td>internetspeed=<%=@avg8%> </td> > > > > > > Average <%=((@avg1+@avg2+@avg3+@avg4+@avg5+@avg6+@avg7+@avg8 > )/8)%> > <% end %> > > </tr> > </table> > > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/wUYDoovBR1UJ. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- Fernando Almeida www.fernandoalmeida.net -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, May 20, 2012 at 10:09 AM, Fernando Almeida < fernando-7WGqr3rU1tV1NwFxuVVnt9HuzzzSOjJt@public.gmane.org> wrote:> You should have a nil value to any @avg, try to use > > @avgN = @school.rate_schools.average(:X) || 0 > > >I also suggest to pass all of that logic to the model so after finding the school: in controller @school = School.find(params[:id]) @avg1 = @school.food_average in model def food_average rate_school.average(:food) end -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks a lot......it''s working.....great group! On Sun, May 20, 2012 at 11:09 AM, Fernando Almeida < fernando-7WGqr3rU1tV1NwFxuVVnt9HuzzzSOjJt@public.gmane.org> wrote:> You should have a nil value to any @avg, try to use > > @avgN = @school.rate_schools.average(:X) || 0 > > > > > 2012/5/20 Roy <poddojaroy-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > >> I am passing hard time with ruby on rails ...I was trying to count the >> average of a whole table......at first I calculated avarage of eah column >> then average of the average values but it is giving me an error- here are >> my codes and error- >> error- >> >> NoMethodError in Rate_schools#index >> >> Showing */home/vmuser/workspace/project3 >> -3/app/views/rate_schools/index.html.erb* where line *#36* raised: >> >> undefined method `+'' for nil:NilClass >> >> Extracted source (around line *#36*): >> >> 33: >> 34: >> 35: >> 36: Average <%=((@avg1+@avg2+@avg3+@avg4+@avg5+@avg6+@avg7+@avg8)/8)%> >> 37: <% end %> >> 38: >> 39: </tr> >> >> >> >> codes In controller: >> def index >> >> @school=School.find(params[:id]) >> @rate_school =@school.rate_schools.build >> >> >> @avg1=@school.rate_schools.average(:library) >> @avg2=@school.rate_schools.average(:careercenter) >> @avg3=@school.rate_schools.average(:location) >> @avg4=@school.rate_schools.average(:food) >> @avg5=@school.rate_schools.average(:healthcenter) >> @avg6=@school.rate_schools.average(:studentactivities) >> @avg7=@school.rate_schools.average(:facilities) >> @avg8=@school.rate_schools.average(:internetspeed) >> >> codes in index : >> <% if !@school.rate_schools.blank? %> >> >> <td> library=<%=@avg1%> </td> >> <td>careercenter=<%=@avg2%> </td> >> <td>location=<%=@avg3%> </td> >> <td>Food=<%=@avg4%> </td> >> <td>Healthcenter=<%=@avg5%> </td> >> <td>studentactivities=<%=@avg6%> </td> >> <td>facilities=<%=@avg7%> </td> >> <td>internetspeed=<%=@avg8%> </td> >> >> >> >> >> >> Average <%=((@avg1+@avg2+@avg3+@avg4+@avg5+@avg6+@avg7+@avg8 >> )/8)%> >> <% end %> >> >> </tr> >> </table> >> >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Talk" group. >> To view this discussion on the web visit >> https://groups.google.com/d/msg/rubyonrails-talk/-/wUYDoovBR1UJ. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> > > > > -- > Fernando Almeida > www.fernandoalmeida.net > > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.