bre*_*ter 0 console data-migration ruby-on-rails
所以我试图在迁移脚本中迁移一些数据,但数据似乎没有保存.但是,如果我复制代码并直接在控制台中运行它,它会保存.任何人都可以帮我找出原因吗?
这是我的迁移脚本中的代码.我正在将我的头像数据从自己的表格移动到我的个人资料表中.
def self.up
add_column :users, :featured, :boolean, :default => false
add_column :profiles, :avatar_file_name, :string
add_column :profiles, :avatar_content_type, :string
add_column :profiles, :avatar_file_size, :integer
add_column :profiles, :avatar_updated_at, :datetime
Avatar.all.each do |a|
user = User.find(a.user_id)
user.profile.avatar_file_name = a.avatar_file_name
user.profile.avatar_content_type = a.avatar_content_type
user.profile.avatar_file_size = a.avatar_file_size
user.profile.avatar_updated_at = a.updated_at
if a.featured == true
user.featured = true
end
user.save
end
# drop_table :avatars
end
Run Code Online (Sandbox Code Playgroud)
我认为这是因为您正在更改列并尝试在相同的迁移中使用它们,但模型不知道字段.
尝试在Avatar.all.each上面添加这些行...
User.reset_column_information
Profile.reset_column_information
Run Code Online (Sandbox Code Playgroud)
更多关于这个http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-reset_column_information
归档时间: |
|
查看次数: |
543 次 |
最近记录: |