Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"config.model" raises "table doesn't exist" exception if database is dropped #289

Closed
krisleech opened this issue Feb 16, 2011 · 20 comments
Closed
Milestone

Comments

@krisleech
Copy link

initializers/rails_admin.rb

config.model Competition do
end

then

rake db:drop
rake db:create
rake db:migrate

In fact any rake task (which loads the environment) whic is run after dropping the database fails. This would appear to be because "config.model" needs to call Competition, I assume to get a list of database fields, and because the competitions table does not exist ActiveRecord raises an exception.

The solution I am using so far is to wrap the config.model block like so:

if defined? Competition
  config.model Competition
  end
end

Ruby 1.9.2
Rails 3.0.3
rails_admin HEAD

@sferik
Copy link
Collaborator

sferik commented Mar 29, 2011

Thanks for the bug report. This needs to be fixed before we officially release RailsAdmin as a gem.

@krisleech
Copy link
Author

Agreed, the workaround I proposed does not work in all situations either. Competition is always defined, and soon as it is inspected the 'no table' exception is raised.

The other workaround I have tried is wrapping the config.model block in an rescue and silently ignoring the exception. Maybe this could be done internally, rescue just the exception in question, and log to the database.

@sdrew
Copy link

sdrew commented Apr 27, 2011

If it helps, here's the way I'm working around it:

if Competition.table_exists?
  config.model Competition
  end
end

@niedfelj
Copy link

Yes, but even if the table exists, if you add columns and define them in a single push to a site like Heroku, you will still have problems when you try to run the rake task to migrate. For example, I added a :body field to a table and put in the rails admin config to have that field show the ckeditor. After pushing to Heroku, I was unable to run the rake task because it would fail on the rails admin config since the field didn't exist yet that rails admin was trying to configure.

@spint
Copy link

spint commented Jun 21, 2011

This workaround does not work for me (same error: table does not exist during migration):

if Competition.table_exists?

It does seem to work with:

if defined? Competition

ruby 1.9.2
rails 3.0.9

@netmute
Copy link

netmute commented Jun 23, 2011

if Competition.table_exists?

works for me.

if defined? Competition

does not.

ruby 1.9.2
rails 3.0.7

This is a serious showstopper :)

@kaapa
Copy link
Collaborator

kaapa commented Jun 23, 2011

Would it be a sensible solution to move from using an initializer to an active record class method for model specific configuration such as:

in app/models/competition.rb

class Competition
  rails_admin do
    # What ever was done in an initializer with config.model method
  end
end

In that case the Competition model wouldn't get loaded due to RailsAdmin, right?

@sferik
Copy link
Collaborator

sferik commented Jun 23, 2011

@kaapa 👍 Sounds good to me. :)

@bbenezech
Copy link
Collaborator

👍 I second that. Is it possible to make it work with class reload to true?

@kaapa
Copy link
Collaborator

kaapa commented Jul 4, 2011

@bbenezech: I've added a configuration reset on Railtie to_prepare hook which is run per request in development and on startup in other environments. Is that what you mean?

@bbenezech
Copy link
Collaborator

It's perfect!

@gunn
Copy link
Collaborator

gunn commented Jul 4, 2011

Looks good. Is it possible to have multiple rails_admin blocks in the same model that would add to each other?

The advantage to this is that it would let us use modules to mixin admin behaviour.

bbenezech added a commit that referenced this issue Jul 4, 2011
bbenezech added a commit that referenced this issue Jul 4, 2011
bbenezech added a commit that referenced this issue Jul 4, 2011
@benlangfeld
Copy link
Contributor

Why was this reverted?

@gunn
Copy link
Collaborator

gunn commented Jul 7, 2011

@benlangfeld: #515

@bbenezech
Copy link
Collaborator

duplicate

@lucasmazza
Copy link
Contributor

Hi,

Even with a rails_admin block inside the model class any attempt to load it without the db still crashes the app - but ActiveRecord let's you load the model and only throws an exception when some action is taken (queries or reading an attribute, for instance). On my app we have a devise_for call that loads the User model and crashes even when running rake tasks due to the admin configuration - without the rails_admin block everything works fine.

Any similar gem that uses some sort of model configuration unrelated to the database on it's initialization can face this same issue. What do you guys think about patching RailsAdmin::Config.model to emits a warning if the table isn't present and just moving along?

@krisleech
Copy link
Author

I think that is a fine solution, generally you will know the table does not exist anyway, as you'll run into this issue running a migration.

@marioizquierdo
Copy link

There is a workaround here:
http://stackoverflow.com/questions/3790867/ruby-on-rails-run-a-method-on-server-start-2-3

To do not run the initializer on rake tasks.

@michikono
Copy link

I have a solution that suppresses the RailsAdmin initializer during rake tasks. I modified my rails_admin.rb initializer as follows:

FROM:

RailsAdmin.config do |config| 
  # stuff
end

TO:

if File.basename($0) == 'rake'
  puts "Skipping RailsAdmin.config due to File.basename($0): " + File.basename($0)
else
  RailsAdmin.config do |config| 
    # stuff
  end
end

Note that rails generate would still get caught in this, but I figure so long as you can migrate, those issues should probably not be as big of a deal.

@jerefrer
Copy link

Works perfectly, much thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests