Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1factory-botattr-protected

Setting protected attributes with FactoryGirl


FactoryGirl won't set my protected attribute user.confirmed. What's the best practice here?

Factory.define :user do |f|
  f.name "Tim"          # attr_accessible -- this works
  f.confirmed true      # attr_protected -- doesn't work
end 

I can do a @user.confirmed = true after using my factory, but that's a lot of repetition across a lot of tests.


Solution

  • Using an after_create hook works:

    Factory.define :user do |f|
      f.name "Tim"
      f.after_create do |user|
        user.confirmed = true
        user.save
      end
    end