{"comment":{"created_at":"2011-10-18T23:27:31Z","body":"Thanks for responding @grosser.\r\n\r\nSetting the db column default to 0 allows the above spec to pass, but it causes this spec to fail:\r\n\r\n```ruby\r\n  describe \"set_email_prefs_to_defaults\" do\r\n    it \"should set the email preferences to the defaults on create\" do\r\n      user = Factory(:user)\r\n      user.credits_email.should == true\r\n      user.weekly_results_email.should == true\r\n      user.challenges_email.should == true\r\n      user.jackpot_results_email.should == true\r\n    end\r\n  end\r\n```\r\n\r\nbecause `attribute_present?(:email_preferences)` in my previous `set_email_prefs_to_defaults` method returns `true` due to the db column default.\r\n\r\nI did, however, find a nice solution that allows both specs to pass:\r\n\r\n```ruby\r\nclass User < ActiveRecord::Base\r\n  ...\r\n  after_initialize :set_email_prefs_to_defaults\r\n  ...\r\n  def set_email_prefs_to_defaults\r\n    self.credits_email ||= true\r\n    self.weekly_results_email ||= true\r\n    self.challenges_email ||= true\r\n    self.jackpot_results_email ||= true\r\n  end\r\nend\r\n```","updated_at":"2011-10-18T23:27:31Z","url":"https://api.github.com/repos/grosser/bitfields/issues/comments/2449250","id":2449250,"user":{"avatar_url":"https://secure.gravatar.com/avatar/ef723187034cdb6795c4fd6674860ada?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"ef723187034cdb6795c4fd6674860ada","url":"https://api.github.com/users/zenprogrammer","id":103835,"login":"zenprogrammer"}},"action":"created","issue":{"number":5,"created_at":"2011-10-17T21:48:29Z","pull_request":{"diff_url":null,"patch_url":null,"html_url":null},"body":"Hi!  Thanks for the handy gem.\r\n\r\nI'm using `bitfield` in my `User` class as follows:\r\n\r\n```ruby\r\nclass User < ActiveRecord::Base\r\n  ...\r\n  bitfield :email_preferences, 1 => :credits_email, 2 => :weekly_results_email, 4 => :challenges_email, 8 => :jackpot_results_email\r\n  ...\r\n  before_create :set_email_prefs_to_defaults\r\n  ...\r\n  def set_email_prefs_to_defaults\r\n    if !attribute_present?(:email_preferences)\r\n      self.credits_email = true\r\n      self.weekly_results_email = true\r\n      self.challenges_email = true\r\n      self.jackpot_results_email = true\r\n    end\r\n  end\r\nend\r\n```\r\n\r\nUnfortunately, this spec is failing:\r\n\r\n```ruby\r\n  describe \"set_email_prefs_to_defaults\" do\r\n    it \"should not set the email preferences to the defaults on create if they're already set\" do\r\n      user = Factory(:user, :credits_email => false, :weekly_results_email => false,\r\n                            :challenges_email => false, :jackpot_results_email => false)\r\n      user.credits_email.should == false\r\n      user.weekly_results_email.should == false\r\n      user.challenges_email.should == false\r\n      user.jackpot_results_email.should == false\r\n    end\r\n  end\r\n```\r\n\r\nI believe this is due to this behavior:\r\n\r\n```ruby\r\nUser.new(:credits_email => false, :weekly_results_email => false, :challenges_email => false, :jackpot_results_email => false).attribute_present?(:email_preferences)  #=> false\r\nUser.new(:credits_email => false, :weekly_results_email => false, :challenges_email => false, :jackpot_results_email => false).email_preferences  #=> nil\r\n```\r\n\r\nIt seems as though the bitfield is not being set when all bits are set to false.  Unless I'm mistaken, this does not seem to be desired behavior.  I'm using version 0.4.0.  Any thoughts/help would be appreciated.  Thanks!","comments":2,"title":"Bitfield not being set when all bits are 0","updated_at":"2011-10-18T23:27:31Z","url":"https://api.github.com/repos/grosser/bitfields/issues/5","id":1933852,"assignee":null,"milestone":null,"closed_at":null,"user":{"avatar_url":"https://secure.gravatar.com/avatar/ef723187034cdb6795c4fd6674860ada?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"ef723187034cdb6795c4fd6674860ada","url":"https://api.github.com/users/zenprogrammer","id":103835,"login":"zenprogrammer"},"html_url":"https://github.com/grosser/bitfields/issues/5","labels":[],"state":"open"},"legacy":{"issue_id":1933852,"comment_id":2449250}}