我可以在FactoryGirl中有一个名为“status”的列吗?

时间:2014-12-15 20:37:19

标签: ruby-on-rails ruby-on-rails-3.2 factory-bot

我有以下代码:

应用程序/模型/ thing.rb

class Thing < ActiveRecord::Base
  attr_accessible :thing_id, :status
end

规格/工厂/ things.rb

FactoryGirl.define do

  sequence :thing_id do |n|
    n
  end

  factory :thing do
    thing_id {generate :thing_id}
    status "Success"
  end
end

规格/控制器/ my_controller / spec.rb

describe MyController do
  describe 'GET /index' do
    it 'must have a status' do
      FactoryGirl.create(:thing, status: 'Success')
      get :index
      expect(response).to be_success
      # there's more here but it's not relevant to this question
    end
  end
end

当我运行它时,我收到以下错误:

Failure/Error: FactoryGirl.create(:thing, status: 'Success')
     NoMethodError:
       undefined method `status=' for #<Thing:0x0000323493ce28>

这似乎是因为&#34;状态&#34;是ActiveRecord或FactoryGirl中的保留字。有没有办法获得一个名为&#34; status&#34;?

的列

1 个答案:

答案 0 :(得分:2)

我在迁移中错过了该列,因此它在模型中不存在。

务必检查您的迁移!