Closed firedev closed 8 years ago
I believe ActiveType is casting "true"
to true
, not 1
(see specs
Can you please post a code example showing the issue?
class Test < ActiveType::Object; attribute :test, :boolean; end
t = Test.new
t.test = "false"
t.test
=> 0
t.test = "true"
t.test
=> 0
require 'rails'
require 'active_type'
ActiveRecord::Base.establish_connection(
adapter: "mysql",
host: "localhost",
username: "root",
password: "",
database: "caida_development"
)
class Foo < ActiveType::Object
attribute :accept_terms, :boolean, default: proc { true }
end
f = Foo.new accept_terms: '1' # => #<Foo >
f.accept_terms # => 1
f.accept_terms.class # => Fixnum
f.accept_terms == true # => false
Thanks, but in my case this comes from an external source as "true"
and "false"
.
Hopefully fixed in 0.4.3.
ActiveRecord converts string values of
"false"
and"true"
tofalse
andtrue
for boolean fields in models.ActiveType on the other hand assigns
0
to boolean field, in any case, which is not correct either, after all strings are considered truthy in Ruby.I believe booleans should contain
true
orfalse
instead of0
and1
.Type-casting behaviour is something I can live with, but I would like to hear you comments what do you think is the best course of action. I am parsing these values from XML so they are coming up as strings. As of now I am probably going to change ActiveType attribute to string and then pass to ActiveRecord as is.