Rake db: migrate fails when using devise_for for models using class methods that end up in db

My User model uses the column_names method allowing mass_assignment for all columns for admins.

class User < ActiveRecord::Base

  attr_accessible :email, :password, :password_confirmation, :remember_me
  attr_accessible *column_names, as: :admin 

end

      

However, this does not work very well with my route file which worked out the config

Sandbox::Application.routes.draw do
  devise_for :users
end

      

That is, when I want to create my users table via "rake db: migrate RAILS_ENV = test". I get

rake aborted!
Mysql2::Error: Table 'sandbox_test.users' doesn't exist: SHOW FULL FIELDS FROM `users`

      

Full trace at http://pastie.org/3748502

I know the table does not exist, so I need to run my migration, but for some reason, rake db: migrate RAILS_ENV = test reloads routes, thereby calling devise_for, and devise_for loads the User class when adding a development mapping, thereby calling the column_names method in a table that doesn't exist yet.

So I'm not sure how to do this? Should I add add if table_exists? every time i want to use column_names method? or is there a better way to handle this problem?

+3


source to share


1 answer


I think the cleanest way to do this is to list the attributes you want admins to use. Testing for the existence of a table or doing something with rescue will probably overshadow your intentions.



0


source







All Articles