Giter VIP home page Giter VIP logo

Comments (5)

grosser avatar grosser commented on August 30, 2024

is User.new.email_preferences == 0 ?

if not, change_column :users, :email_preferences, :integer, :default => 0, :null => false

from bitfields.

rroblak avatar rroblak commented on August 30, 2024

Thanks for responding @grosser.

Setting the db column default to 0 allows the above spec to pass, but it causes this spec to fail:

  describe "set_email_prefs_to_defaults" do
    it "should set the email preferences to the defaults on create" do
      user = Factory(:user)
      user.credits_email.should == true
      user.weekly_results_email.should == true
      user.challenges_email.should == true
      user.jackpot_results_email.should == true
    end
  end

because attribute_present?(:email_preferences) in my previous set_email_prefs_to_defaults method returns true due to the db column default.

I did, however, find a nice solution that allows both specs to pass:

class User < ActiveRecord::Base
  ...
  after_initialize :set_email_prefs_to_defaults
  ...
  def set_email_prefs_to_defaults
    self.credits_email ||= true
    self.weekly_results_email ||= true
    self.challenges_email ||= true
    self.jackpot_results_email ||= true
  end
end

from bitfields.

grosser avatar grosser commented on August 30, 2024

setting bitfield columns before create / initialize is a complicated topic,
but i think you almost nailed it, try before_initialize, then even

User.new(:credits_email=>false).credits_email.should == false

from bitfields.

grosser avatar grosser commented on August 30, 2024

There is no before_initialize ...
I added a spec for how it should work, but so far its failing, maybe you can find a way to fix it :)
2170dc5

from bitfields.

grosser avatar grosser commented on August 30, 2024

closing this, doing false ||= true is true ... no bug here ...

from bitfields.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.