Re: Libs directory

Eduardo Yáñez Parareda
2008-07-10T16:19:49Z

Well, I''m feeling stupid....

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

On Jul 10, 6:04 pm, Eduardo Yáñez Parareda
<eduardo.ya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> > 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.