3

I've just run a migration to add a :new_column to :my_table. This is a serialized column (first one in entire project), so I've added the configuration to the model:

serialize :new_column, Array

My migration was as the following:

add_column :my_table, :new_column, :text

After that, I've run rake db:reset, so as to seed new data, and checked in rails console to ensure the data was properly added to my :new_column. And it's all there.

So far so good, but... inside rails server, my application will simply halt with the error:

ActiveModel::MissingAttributeError in TestController#index
missing attribute: new_column

Well, all I know is that my controller is trying to access @my_table_model.new_column. I've also checked schema.db, and everything is fine there.

What could be causing it?

Alvaro Lourenço
  • 1,321
  • 1
  • 10
  • 21
  • Are you operating in the development environment (as opposed to test or production) from both the rails console and the rails server? If your test database is different from your development database, did you apply the changes to both databases? – Whit Kemmey May 08 '13 at 23:44
  • Yeah... I'm running all in development only. See message from console: `Loading development environment (Rails 3.2.9)`; and message from server: `=> Rails 3.2.9 application starting in development on http://0.0.0.0:3000`. Console access the data and server halts with `ActiveModel::MissingAttributeError` :S – Alvaro Lourenço May 09 '13 at 00:13
  • And you restarted the rails server after applying the migration? – Whit Kemmey May 09 '13 at 00:40
  • Sure... and thanks for being so helpful. But I'm using the same terminal to move around migration, console and server. So I have to close one to go another... and I've already done it a bunch of times! Maybe is there a particular procedure to "truly" reset the server that I am not aware of? – Alvaro Lourenço May 09 '13 at 00:57
  • No. I figured you would have restarted, but it's the only other thing I could think of. I'm at a loss, but the next guy that looks at this won't have to ask :) – Whit Kemmey May 09 '13 at 01:00
  • Not sure if you have read this or whether it helps: http://stackoverflow.com/a/6847501/559119 – Sun May 09 '13 at 03:22

2 Answers2

4

Solved! It was a Rails.cache issue.

Database was indeed being populated, but some property from prior schema was cached, and when controller tried to access, a previous object was being returned (one without the :new_column_field). So the final solution was pretty simple:

  Rails.cache.clear

Thanks Whit and Sunxperous, for helping me out. :)

Community
  • 1
  • 1
Alvaro Lourenço
  • 1,321
  • 1
  • 10
  • 21
0

Also be sure that you are not using database schema cache dump "feature".

For example for very large deployments where many pods/servers/unicorns/workers/threads are accessing to same database, could be useful to create a schema dump with:

RAILS_ENV=$RAILS_ENV bundle exec rake db:schema:cache:dump

This is useful to reduce accesses to database during deploys and so to improve performances.

But then you must be sure that new pods/servers are launched AFTER all migrations were completed, if not, that schema cache contains old references to database.

More details here: https://kirshatrov.com/2016/12/13/schema-cache/

Diego D
  • 1,706
  • 19
  • 23