christos / attribute_choices

Map ActiveRecord attribute values to display values for consumption by humans

Home Page:http://github.com/christos/attribute_choices/tree/master

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

error when running migration to add attribute

drewda opened this issue · comments

When I try to apply a migration that will add an attribute to an existing model and I put attribute_choices code in that model to go with the new attribute, the migration fails when it try to load ActiveRecord. ActiveRecord complains that the attribute does not already exist and so it never even runs the migration.

This is on Rails 3.2.8.

I am afraid I can't reproduce it.

ActiveRecord shouldn't load the model unless you are actually using it in the migration that adds the column. That is usually bad form. It is better to have two migration, one that changes the columns and one that does any data migrations and uses the changed module.

Also, have you looked at ActiveRecord::Base.reset_column_information? This forces AR to pick up the new column straight after the add_column is called.

Having said all that, can you send me a simple step by step guide for a simple one model app to demonstrate your problem?

I tried the following in order to reproduce it:

rails new wadusapp && cd wadusapp

echo "gem 'attribute_choices' >> Gemfile && bundle

script/rails generate resrouce Post title:string
rake db:migrate

sg migration AddFormatToPosts format:string

# Add the next line to post.rb:
# attribute_choices :format, [['md', 'Markdown'],['textile', 'Textile']]

rake db:migrate

We are facing the same issue, any solution? we are using Rails 3.2.3

@henrydjacob Did you try my suggestion of running ActiveRecord::Base.reset_column_information in your migration after you add the column?

I would love some steps or a sample app to help me reproduce this bug