# References in Rails 2.0 migrations

**URL:** <https://www.ruby-forum.com/t/references-in-rails-2-0-migrations/124862>\
**Category:** Rails\
**Created:** [January 3, 2008, 8:26am UTC](https://www.ruby-forum.com/t/references-in-rails-2-0-migrations/124862 "2008-01-03T08:26:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alfredo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@Alfredo](https://www.ruby-forum.com/u/Alfredo)\
**Post date:** [January 3, 2008, 8:26am UTC](https://www.ruby-forum.com/t/references-in-rails-2-0-migrations/124862/1 "2008-01-03T08:26:49Z")

</div>

I’ve searched around and have not found a way to add a reference  
column to an existing table, I guess it would be something like

class AddProjectReferenceToTask \< ActiveRecord::Migration  
def self.up  
add\_reference :tasks, :projects

end

def self.down  
remove\_reference :tasks, :projects  
end  
end

of course the methods don’t exist but you get an idea of what the  
problem is, man I need that AWDR for 2.0…

---

<div class="post-metadata">

**Author:** ![Alfredo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@Alfredo](https://www.ruby-forum.com/u/Alfredo)\
**Post date:** [January 3, 2008, 12:31pm UTC](https://www.ruby-forum.com/t/references-in-rails-2-0-migrations/124862/2 "2008-01-03T12:31:05Z")

</div>

Um, you mean an association, right?

I’m assuming it’s a HABTM one,  
def self.up  
create\_table :projects\_tasks, :id =\> false do |t|  
t.integer :project\_id, :task\_id  
end  
end  
self

def self.down  
drop\_table :projects\_tasks  
end

---

<div class="post-metadata">

**Author:** ![Alfredo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@Alfredo](https://www.ruby-forum.com/u/Alfredo)\
**Post date:** [January 4, 2008, 5:24am UTC](https://www.ruby-forum.com/t/references-in-rails-2-0-migrations/124862/3 "2008-01-04T05:24:14Z")

</div>

now I see what the problem was, I thought that by doing  
t.references :project the migration would do the reference on mysql,  
instead of just creating the field for the FK.

It’s a one to many so all I had to do to add the reference was

add\_column :tasks, :project\_id, :integer
