1

We just added a couple of observers to our Rails application. Now, when running migrations from scratch we get an error saying that a table doesn't exist (duh, we haven't migrated yet). The error is thrown because a plugin in a model is being loaded that asks for column_names.

I am assuming that the observers are causing the models to be loaded because when we comment out the observers line in application.rb, the error is not thrown.

How do I run migrations without loading observers and models?

or

How do I ask for the column_names in my plugin in a way that won't throw an error when running migrations?

Sixty4Bit
  • 12,852
  • 13
  • 48
  • 62

3 Answers3

5

In Rails 3 you can skip adding the observers by determining if Rake is being run:

config.active_record.observers = :my_model_observer unless File.basename($0) == 'rake'

This turns off the observers, which means the models and plugins aren't loaded.

Sixty4Bit
  • 12,852
  • 13
  • 48
  • 62
1

I haven't found a decent way to disable Observers at run time. This has been previously discussed in Simple way of turning off observers during rake task?

However, I guess you could "unplug" the plugin code from your model by redefining the troublesome model in your migration:

class YourMigration < ActiveRecord::Migraation
  class YourModel < ActiveRecord::Base; end

  def self.up
    ...
  end

  def self.down
    ...
  end
end
Community
  • 1
  • 1
Innerpeacer
  • 1,321
  • 12
  • 20
  • Going back and tweaking all of my migrations probably isn't a good way forward, but thanks for the link to the other question. – Sixty4Bit Sep 13 '11 at 13:57
  • The link actually provided this awesome fix for the config: config.active_record.observers = :my_model_observer unless File.basename($0) == 'rake' – Sixty4Bit Sep 20 '11 at 16:15
  • It is not a way of disabling observers at run time, but yes, I didn't realize it might be just a good solution in your situation. – Innerpeacer Sep 20 '11 at 23:36
0

In your model you can catch the specific exception generated when you run migrations.

Richard.P
  • 802
  • 7
  • 6
  • I might catch the exception in the plugin, but definitely not the model. – Sixty4Bit Sep 12 '11 at 22:38
  • I dont think i understood well, what do you mean by "a plugin in a model is being loaded that asks for column_names", where is the column_names method called ? and btw what plugin is it ? – Richard.P Sep 12 '11 at 23:56
  • We wrote the plugin, it isn't public. Part of what it does is check to see if the Model that includes it has specific columns. – Sixty4Bit Sep 13 '11 at 00:06
  • Ok, so in the plugin you can check if the table exists before calling column_names, something like ActiveRecord::Base.connection.tables.include?(table_name), you can encapsulate this in a table_exists? method in your plugin. – Richard.P Sep 13 '11 at 00:19
  • Humorously table_exists? Already exists as part of ActiveRecord::Base. The issue isn't whether or not it exists or not. The plugin should not be loaded while doing migrations. I am trying to avoid a check because I want it to fail noisily if the table or column doesn't exist. – Sixty4Bit Sep 13 '11 at 13:55