2015-09-29 21 views
1

我有一個Rails遷移文件Rspec的加載過去(錯誤)型號

class CreateUserData < ActiveRecord::Migration 
    def change 
    create_table :user_data do |t| 
     t.belongs_to :user, index: true 
     t.string :country 
     t.string :city 
     t.string :state 
     t.string :language 
     t.string :device_advertising_id 
     t.string :client_type 
     t.string :data_type 

     t.timestamps 
    end 
    end 
end 

我用它玩了幾次。然後,我改變它。

class CreateUserData < ActiveRecord::Migration 
    def change 
    create_table :user_data do |t| 
     t.belongs_to :user, index: true 
     t.string :country 
     t.string :city 
     t.string :sublocality # added 
     t.string :zip_code # added 
     t.string :language 
     t.string :device_advertising_id 
     t.string :client_type 
     t.string :data_type 

     t.timestamps 
    end 
    end 
end 

這是模型文件。

# a class to store user data based on initial and latest 
class UserData < ActiveRecord::Base 
    belongs_to :user, class_name: 'Spree::User' 
    validates :device_advertising_id, presence: true 

    enum data_type: { initial: 'initial', latest: 'latest' } 

    scope :no_initial, -> { where(device_advertising_id: device_advertising_id).where(data_type: 'initial') } 

    def first_update(country='', city='', sublocality='', zip_code='', language='', client_type='') 
    self.country ||= country 
    self.city ||= city 
    self.sublocality ||= sublocality 
    self.zip_code ||= zip_code 
    self.language ||= language 
    self.client_type ||= client_type 
    end 
end 

當我檢查鐵軌控制檯

的UserData(ID上的UserData模式:整數,USER_ID:整數,國家:字符串,城市:字符串, sublocality:字符串,ZIP_CODE:字符串,語言:字符串, device_advertising_id:字符串,client_type:字符串,DATA_TYPE:字符串, created_at:日期時間,的updated_at:DATETIME)

但是,當我跑rspec的。我的工廠失敗。 undefined method sublocality= for #<UserData:0x007fd144230830>

當我跑過畢業並檢查課程。

的UserData(ID:整數,USER_ID:整數,國家:字符串,城市:字符串, 狀態:字符串,語言:字符串,device_advertising_id:字符串, client_type:字符串,created_at:日期時間,的updated_at:DATETIME)

任何想法爲什麼rspec總是加載過去的類的版本?我已經遷移到最新的遷移並檢查了數據庫表。

回答

1

對不起,我忘了Rails測試數據庫與開發的不同。我只需要跑步。

rake db:rollback RAILS_ENV=test 
rake db:migrate RAILS_ENV=test 

使其工作。

+0

很酷。你可以點擊「接受」來將你自己的答案標記爲已接受的答案嗎?謝謝, – Felix

+0

但是,我需要等待兩天。 :)。 –