How to use FactoryGirl with Rails 3.1 attr_accessible ?

261 views
Skip to first unread message

Man Vuong Ha Thanh

unread,
Sep 28, 2012, 8:06:23 AM9/28/12
to factor...@googlegroups.com
I have a model like this:
class Customer < ActiveRecord::Base
  attr_accessible :name
  attr_accessible :name, :credit_rating, :as => :admin
end

To set the credit_rating attribute we must do:
c = Customer.create!({
:name => "Customer", :credit_rating => 2.5
}, :as => :admin)

How can we define this in Factory?

Thanks.

Josh Clayton

unread,
Sep 28, 2012, 8:15:51 AM9/28/12
to factor...@googlegroups.com
FactoryGirl assigns each attribute directly, which bypasses attr_accessible. The article here: https://github.com/thoughtbot/factory_girl/wiki/How-factory_girl-interacts-with-ActiveRecord explains how FactoryGirl and ActiveRecord interact more explicitly, but declaring the factory as

factory :customer do
  name 'My wonderful customer'
  credit_rating 2.5
end

should be fine.

Best of luck!
Reply all
Reply to author
Forward
0 new messages