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

Closed
krisleech opened this Issue Feb 16, 2011 · 20 comments

Projects

None yet
@krisleech

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
Owner
sferik commented Mar 29, 2011

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

@krisleech

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
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

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
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
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
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
Owner
sferik commented Jun 23, 2011

@kaapa 👍 Sounds good to me. :)

@bbenezech
Collaborator

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

@kaapa
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
Collaborator

It's perfect!

@gunn
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 bbenezech added a commit that referenced this issue Jul 4, 2011
@bbenezech bbenezech Allow configuration within model classes, discussed in #275 and #289.…
… Fixes #275

Conflicts:

	README.md
fb33469
@bbenezech bbenezech added a commit that referenced this issue Jul 4, 2011
@bbenezech bbenezech Revert "Allow configuration within model classes, discussed in #275 and
#289. Fixes #275"

This reverts commit fb33469.
9b008d1
@bbenezech bbenezech added a commit that referenced this issue Jul 4, 2011
@bbenezech bbenezech Revert "Allow configuration within model classes, discussed in #275 and
#289. Fixes #275"

This reverts commit d035100.
2de1e42
@benlangfeld
Contributor

Why was this reverted?

@gunn
Collaborator
gunn commented Jul 7, 2011
@bbenezech
Collaborator

duplicate

@bbenezech bbenezech closed this Jul 7, 2011
@lucasmazza
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

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.

@mshibuya mshibuya added a commit to mshibuya/rails_admin that referenced this issue Aug 17, 2011
@kaapa @mshibuya kaapa + mshibuya Allow configuration within model classes, discussed in #275 and #289.…
… Fixes #275
b3af868
@marioizquierdo

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

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

Works perfectly, much thanks !

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