Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload column names in ActiveRecord model class

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

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?

like image 801
agentofuser Avatar asked Nov 18 '09 14:11

agentofuser


1 Answers

Legislator.reset_column_information (API info)

like image 90
agentofuser Avatar answered Nov 15 '22 21:11

agentofuser