Libs directory

Hello,

I had an application on Rails 1.2 or 1.1 (don't remember). Now I have
updated it to 2.1.

I had a migrations_helper.rb in the Libs directory which defined
foreign_key and drop_foreign_key methods. But now migrate task doesn't
find them. Have I to include this directory somewhere with Rails 2.1?
Doesn't it work as always?

Could I use another thing to declare foreign keys without having a
class made by myself?

Any suggestion?

Eduardo Yáñez Parareda wrote:

Hello,

I had an application on Rails 1.2 or 1.1 (don't remember). Now I have
updated it to 2.1.

I had a migrations_helper.rb in the Libs directory which defined
foreign_key and drop_foreign_key methods. But now migrate task doesn't
find them. Have I to include this directory somewhere with Rails 2.1?
Doesn't it work as always?

Could I use another thing to declare foreign keys without having a
class made by myself?

Depends how your code works,
but I imagine you're extending the existing rails migration classes.

so I'd suggest doing a "require 'migration_extensions'" at the top of
the migration file.

Depends how your code works,
but I imagine you're extending the existing rails migration classes.

so I'd suggest doing a "require 'migration_extensions'" at the top of
the migration file.

Yes, I already do it!...

This is my helper:

module MigrationHelpers
  def foreign_key(from_table, from_column, to_table)
    constraint_name = "fk_#{from_table}_#{from_column}"
    execute %{alter table #{from_table}
              add constraint #{constraint_name}
              foreign key (#{from_column})
              references #{to_table} (id)}
  end

  def drop_foreign_key(table, foreign_key)
    execute %{alter table #{table} drop foreign key #{foreign_key}}
  end
end

and this is my migration class:

require 'migration_helpers'

class CreateProjectUserRoles < ActiveRecord::Migration
  def self.up
    create_table :project_user_roles do |t|
      t.references :project
      t.references :user
      t.string :role, :null => false

      t.timestamps
    end
    foreign_key :project_user_roles, :user_id, :users
    foreign_key :project_user_roles, :project_id, :projects
  end

  def self.down

drop_foreign_key :project_user_roles, :fk_project_user_roles_user_id

drop_foreign_key :project_user_roles, :fk_project_user_roles_project_id
    drop_table :project_user_roles
  end
end

Helper is into 'lib' directory, so I think it is loaded when server
starts. Well Indeed it worked in RoR 1.2.

Well, I'm feeling stupid....

I forgot to extend the migration class in order to use the foreign key
Module...