I imported some values from Excel using Roo into boolean fields in my Rails app.
In Rails Console it looks like everything is working fine, but in my Rails App the boolean values don't seem to be read by Rails.
I have a DataPoint model. One attribute of DataPoint is the boolean field currently_employed.
In Rails Console it works like this (seems fine):
003:0 > x.currently_employed
=> true
004:0 > z.currently_employed
=> false
005:0 > x.currently_employed == false
=> false
006:0 > x.currently_employed == "true"
=> false
007:0 > x.currently_employed == true
=> true
008:0 > z.currently_employed == false
=> true
009:0 > z.currently_employed == "false"
=> false
010:0 > z.currently_employed == true
=> false
Given everything seems to look good in Console, I'd have thought that this formula in my data_point.rb model would work fine:
def self.pct_true(data_points)
true_count = 0
data_points.each do |x|
if x.currently_employed
true_count += 1
else
end
end
return true_count / data_points.count * 100
end
But it returns the value zero, even though I have multiple instances of DataPoint where x.currently_employed evaluates to true in Rails Console.
Interestingly, when I change the boolean field "currently_employed" to the integer field "annual_income", this same function counts each instance and returns the value 100.
Given the Rails Console results above, any idea what might be wrong with the method in my model?
Thanks!
Your issue is just in integer division - take a look:
[1] pry(main)> 1 / 100
=> 0
[2] pry(main)> 20 / 100
=> 0
A quick fix might be to do:
return true_count / data_points.count.to_f * 100