Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
2 total  XML / RSS feed 

Uninitialized constant error in Rails migration

This was because I had pluralized Tiers in its class definition, not because Rails was acting different. The below is not true, but it still is a good idea because if you change any models in the future, it will break subsequent migrations (which would likely happen anyways)

Now for some reason you have to define your models within your migration to get access to their functionality. It's not guaranteed that the migration will have automatic access to your models.

class LoadTiersUsers < ActiveRecord::Migration
  extend MigrationHelpers

        class Tier < ActiveRecord::Base; has_and_belongs_to_many :users end
        class User < ActiveRecord::Base; has_and_belongs_to_many :tiers end
        class TiersUsers < ActiveRecord::Base; end
  def self.up
                p "*** Loading user, tier data..."

Creating a table with migrations when using has_and_belongs_to_many

You can find the information at under the "create_table" method. The short answer is this though,

create_table :table_name, :id => false do |t|


:id is the part you're looking for, it defaults to true, which creates the :id as an autoincrementing primary key, if you use migrations and want to create the join table, you don't want an "id" column, so use :id => false to override the create_table method and tell it you don't want that primary key.
« Newer Snippets
Older Snippets »
2 total  XML / RSS feed