ruby - Boolean values not being "read" in Rails -
i imported values excel using roo boolean fields in rails app.
in rails console looks working fine, in rails app boolean values don't seem read rails.
i have datapoint model. 1 attribute of datapoint boolean field currently_employed.
in rails console works (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 seems in console, i'd have thought formula in data_point.rb model work fine:
def self.pct_true(data_points) true_count = 0 data_points.each |x| if x.currently_employed true_count += 1 else end end return true_count / data_points.count * 100 end
but returns value zero, though have multiple instances of datapoint x.currently_employed evaluates true in rails console.
interestingly, when change boolean field "currently_employed" integer field "annual_income", same function counts each instance , returns value 100.
given rails console results above, idea might wrong method in model?
thanks!
your issue in integer division - take look:
[1] pry(main)> 1 / 100 => 0 [2] pry(main)> 20 / 100 => 0
a quick fix might do:
return true_count / data_points.count.to_f * 100
Comments
Post a Comment