grosser/bitfields — Issue #5: Bitfield not being set when all bits are 0
Author: zenprogrammer
Created: 2011-10-17 21:48:29+00:00
State: open

Hi! Thanks for the handy gem.

I'm using `bitfield` in my `User` class as follows:

```ruby
class User < ActiveRecord::Base
...
bitfield :email_preferences, 1 => :credits_email, 2 => :weekly_results_email, 4 => :challenges_email, 8 => :jackpot_results_email
...
before_create :set_email_prefs_to_defaults
...
def set_email_prefs_to_defaults
if !attribute_present?(:email_preferences)
self.credits_email = true
self.weekly_results_email = true
self.challenges_email = true
self.jackpot_results_email = true
end
end
end
```

Unfortunately, this spec is failing:

```ruby
describe "set_email_prefs_to_defaults" do
it "should not set the email preferences to the defaults on create if they're already set" do
user = Factory(:user, :credits_email => false, :weekly_results_email => false,
:challenges_email => false, :jackpot_results_email => false)
user.credits_email.should == false
user.weekly_results_email.should == false
user.challenges_email.should == false
user.jackpot_results_email.should == false
end
end
```

I believe this is due to this behavior:

```ruby
User.new(:credits_email => false, :weekly_results_email => false, :challenges_email => false, :jackpot_results_email => false).attribute_present?(:email_preferences) #=> false
User.new(:credits_email => false, :weekly_results_email => false, :challenges_email => false, :jackpot_results_email => false).email_preferences #=> nil
```

It 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!