# Libs directory

**URL:** <https://www.ruby-forum.com/t/libs-directory/142217>\
**Category:** Rails\
**Created:** [July 4, 2008, 6:17pm UTC](https://www.ruby-forum.com/t/libs-directory/142217 "2008-07-04T18:17:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [July 4, 2008, 6:17pm UTC](https://www.ruby-forum.com/t/libs-directory/142217/1 "2008-07-04T18:17:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [July 7, 2008, 2:09pm UTC](https://www.ruby-forum.com/t/libs-directory/142217/2 "2008-07-07T14:09:15Z")

</div>

Any suggestion?

On Jul 4, 6:17 pm, Eduardo Yáñez Parareda [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [July 7, 2008, 6:30pm UTC](https://www.ruby-forum.com/t/libs-directory/142217/3 "2008-07-07T18:30:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [July 10, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/libs-directory/142217/4 "2008-07-10T18:20:11Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![eyp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@eyp](https://www.ruby-forum.com/u/eyp)\
**Post date:** [July 10, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/libs-directory/142217/5 "2008-07-10T18:04:57Z")

</div>

> 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.
