我有一个Rails迁移文件
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模型在rails控制台
UserData(id: integer, user_id: integer,国家:string,城市:string,子区域:字符串,zip_code:字符串,语言:字符串,Device_advertising_id: string, client_type: string, data_type: string,Created_at: datetime, updated_at: datetime)
但是,当我运行rspec。我的工厂失败了。undefined method sublocality= for #<UserData:0x007fd144230830>
当我运行bybug并检查类时
UserData(id: integer, user_id: integer,国家:string,城市:string,状态:string,语言:string, device_advertising_id: string,Client_type: string, created_at: datetime, updated_at: datetime)
知道为什么rspec总是加载过去版本的类吗?我已经迁移到最新的迁移并检查了数据库表。
对不起,我忘记了Rails测试数据库与开发数据库不同。我只需要跑。
rake db:rollback RAILS_ENV=test
rake db:migrate RAILS_ENV=test