Skip to content

Instantly share code, notes, and snippets.

@haslinger
Last active December 30, 2015 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haslinger/7838114 to your computer and use it in GitHub Desktop.
Save haslinger/7838114 to your computer and use it in GitHub Desktop.
Tricking the migration generator when having two models with same table_namein Hobo

Two models / same table_name in Hobo

I had the following issue:

A hobo model like this:

class Category < ActiveRecord::Base
  hobo_model
  fields do
    ...some fields, nothing special ...
  end

And another model

class Legacy::Category < ActiveRecord::Base
  establish_connection "import_development"
  self.table_name = 'categories'
end
which I use for importing data from a legacy database.

This confuses the heck out of the migration generator and it wants to recreate the table categories in the next migration again. I tried to use Generators::Hobo::Migration::Migrator.ignore_models = ["Legacy:Category"], but that won't help, the migration gerenator seems not to take namespaces into account, Generators::Hobo::Migration::Migrator.ignore_models = ["Category"] would actually silence it, but also disables migrations for future changes of the Category model all together.

But if I make that legacy model a hobo model (without using any of the Hobo features) like this

class Legacy::Category < ActiveRecord::Base
  establish_connection "import_development"
  self.table_name = 'categories'
  # The following two lines fix the migration issues
  hobo_model
  fields 
end

not only the migration generator is "silenced" for the legacy model, it also still works for the original Category model. This may be just a coincidence, but who cares? It works! :-)

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