Re: Libs directory

Eduardo Yáñez Parareda
2008-07-10T16:04:15Z

> 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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org