How to use FactoryGirl with Rails 3.1 attr_accessible ?

Showing 1-2 of 2 messages
How to use FactoryGirl with Rails 3.1 attr_accessible ? Man Vuong Ha Thanh 9/28/12 5:06 AM
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.
Re: How to use FactoryGirl with Rails 3.1 attr_accessible ? Josh Clayton 9/28/12 5:15 AM
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!