刷新在ActiveRecord模型类的列名 [英] Reload column names in ActiveRecord model class

查看:136
本文介绍了刷新在ActiveRecord模型类的列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用ActiveRecord的动态基于从CSV文件中读取值创建列名的脚本,是这样的:

I have a script using ActiveRecord that creates column names dynamically based on values read from a CSV file, something like this:

FasterCSV.foreach('votes.csv', :headers => true) do |row|
  column_name = "roll_call_id_#{row['roll_call_id']}"

  if !Legislator.columns.map(&:name).include?(column_name)
    connection_pool.connection.add_column('legislators', column_name, 'string')
  end
end

的问题是,在创建新的列后,我不能做一个 legislator.update_attribute(列,值),因为该类不拿起新列和抱怨它不存在。

The problem is that, after creating the new column, I can't do a legislator.update_attribute(column_name, value) because the class doesn't pick up the new column and complains it doesn't exist.

我怎样才能使它再次查询表结构?

How can I make it query the table structure again?

推荐答案

Legislator.reset_column_information API帮助

这篇关于刷新在ActiveRecord模型类的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆