brainspec / enumerize

Enumerated attributes with I18n and ActiveRecord/Mongoid support
MIT License
1.73k stars 190 forks source link

enumerize ... multiple: true returns empty Enumerize::Set #116

Closed akirill0v closed 10 years ago

akirill0v commented 10 years ago

When I bring class to the superclass with "becomes" it breaks enumeraize fields. For multiple: true only:

class User < ActiveRecord::Base
  extend Enumerize
  enumerize :sex, :in => [:male, :female]
  serialize :interests, Array
  enumerize :interests, :in => [:music, :sports, :dancing, :programming], :multiple => true
end

class UniqStatusUser < User
end

uniq_user = UniqStatusUser.new
uniq_user.interests = [:sports, :dancing]
uniq_user.sex = :male
uniq_user.save!

user = uniq_user.becomes(User)
user.interests.must_equal uniq_user.interests # => <Enumerize::Set {}>
user.sex.must_equal uniq_user.sex # => "male"
lest commented 10 years ago

I've fixed it in 0826fcb97410b2b5a2cf6042590b14e1722070a4. Please give it a try. Thanks!

PS. #becomes method creates a new record using new call. That's why default value (the empty one in this case) is set and cached. So we need to extend #becomes method to set correct value.

akirill0v commented 10 years ago

@lest thanks a lot! It works now!

fernandodev commented 9 years ago

Hi,

I'm using version 0.11.0 and it occurs

class Message < ActiveRecord::Base
  extend Enumerize

  serialize :devices, Array
  enumerize :devices, in: [:ios, :android], multiple: true

end

after I try to save, devices values won't be saved.

m = Message.new(devices: [:ios, :android])
m.save!
lest commented 9 years ago

@fernandodev I cannot reproduce this issue in tests. I added the test 3cb333526c49a65badd671a1f1638b5f731eb6cb to make sure everything works as expected. Can you please push a test application reproducing it and open a separate issue?

fernandodev commented 9 years ago

@lest I finally found the error. It was a bad migration generation.

I fixed when I saw the test

Thank you

hafizio commented 8 years ago

I'm still having this problem related to this.

  enumerize :role, in: [:admin, :trainer, :company], predicates: true, multiple: true, default: :company

after initialization, I can see the role field has :company value. But after creation, the field becomes empty returning => <Enumerize::Set {}>