TypeError (A string cannot be forced into Fixnum)?

I get a strange error. My application works fine on my localhost, but on my Heroku server it gives this error: TypeError (String can't be coerced into Fixnum):

Here is my code:

 @rep = rep_score(@u) 

According to the magazines, that line throws an error. I commented on this and pushed the changes in Heroku, and the application now works fine.

Here is the rep_score method:

 def rep_score(user) rep = 0 user.badges.each do |b| rep = rep + b.rep_bonus end return rep end 

Also rep_bonus is an integer in the database.

Again this works fine on the local host. Let me know what you think.


After removing return from the rep_score method rep_score it works fine. I'm still new to Ruby, is there something wrong with putting return ? This is a habit from other languages.

+6
source share
1 answer

Ruby uses + as a combination tool for strings AND math situations.

Simple fix:

 def rep_score(user) rep = 0 user.badges.each do |b| rep = rep + b.rep_bonus.to_i end return rep end 

to_i changes rep_bonus , which is probably from the database model, from the result of the string to an integer. There are several different types that you can install. To name a few conversions:

  • Array: to_a
  • Float: to_f
  • Integer: to_i
  • String: to_s
+10
source

Source: https://habr.com/ru/post/920772/


All Articles