# ActiveRecord Migration question

**URL:** <https://discuss.rubyonrails.org/t/activerecord-migration-question/36408>\
**Category:** rubyonrails-talk\
**Created:** [November 16, 2008, 7:51pm UTC](https://discuss.rubyonrails.org/t/activerecord-migration-question/36408 "2008-11-16T19:51:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlb5000](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mlb5000](https://discuss.rubyonrails.org/u/mlb5000)\
**Post date:** [November 16, 2008, 7:51pm UTC](https://discuss.rubyonrails.org/t/activerecord-migration-question/36408/1 "2008-11-16T19:51:42Z")

</div>

I have a database table, users, which has a referrer\_id column. This column maps back to the users table to denote the user who referred this record. How can I accomplish this type of reference with the t.references call in my create\_table without needing to create some sort of crazy (and unnecessary) intermediate table? I'd like the column name to remain referrer\_id so the intention of the column is clear.

Thanks, Matt

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [November 16, 2008, 8:09pm UTC](https://discuss.rubyonrails.org/t/activerecord-migration-question/36408/2 "2008-11-16T20:09:13Z")

</div>

> I have a database table, users, which has a referrer\_id column. This column maps back to the users table to denote the user who referred this record. How can I accomplish this type of reference with the t.references call in my create\_table without needing to create some sort of crazy (and unnecessary) intermediate table? I'd like the column name to remain referrer\_id so the intention of the column is clear.

I'm not sure I understand - you've already got the column so why are you worrying about the migration ? t.references doesn't do any magic - it just adds \_id to the end and creates a column of the right type (IIRC in recent versions of rails if it's also generating the model class it will insert the belongs\_to into the model too)

Anyway the point is that the migration is dumb, it just adds a column. In terms of making that association work your association will need to specify the :class\_name option and so on.

Fred

---

<div class="post-metadata">

**Author:** ![mlb5000](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mlb5000](https://discuss.rubyonrails.org/u/mlb5000)\
**Post date:** [November 16, 2008, 8:27pm UTC](https://discuss.rubyonrails.org/t/activerecord-migration-question/36408/3 "2008-11-16T20:27:14Z")

</div>

So from my understanding of what you're saying, this would be the line in the migration: t.integer :referrer\_id then the users class:

class User \< ActiveRecord::Base &nbsp;&nbsp;has\_one :referrer, :class\_name =\> user end

?

I guess it was misleading to say that the column exists. I've created the database schema via diagrams and am now trying to map it using migrations. Soft of an exercise for me to learn about all this.

Thanks, Matt

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [November 16, 2008, 8:33pm UTC](https://discuss.rubyonrails.org/t/activerecord-migration-question/36408/4 "2008-11-16T20:33:57Z")

</div>

> So from my understanding of what you're saying, this would be the line in the migration: t.integer :referrer\_id then the users class:

that would do it.

> class User \< ActiveRecord::Base has\_one :referrer, :class\_name =\> user end

First off the class\_name option should be a string ie :class\_name =\> 'User'). Secondly, that should be a belongs\_to, ie. belongs\_to :referrer, :class\_name =\> 'User' - if the foreign key is part of the record in question then it's a belongs to (this distinction is clearer if it's not an association on the same table). You might also want a second association that tells you all the people that a given user has referred, for that you'd need something along the lines of

has\_many :somethings, :class\_name =\> 'User', :foreign\_key =\> 'referrer\_id'

Fred

---

<div class="post-metadata">

**Author:** ![mlb5000](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mlb5000](https://discuss.rubyonrails.org/u/mlb5000)\
**Post date:** [November 16, 2008, 8:46pm UTC](https://discuss.rubyonrails.org/t/activerecord-migration-question/36408/5 "2008-11-16T20:46:42Z")

</div>

Great, this is actually exactly what I needed. And just to check my understanding:

belongs\_to is used if this object (table) holds the referencing key has\_one is used if the object (table) you are referencing holds they key back to your id

As in:

The users table also has a country\_id column, so inside the User class I also have

belongs\_to :country

Correct?

Thanks, Matt

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [November 16, 2008, 10:19pm UTC](https://discuss.rubyonrails.org/t/activerecord-migration-question/36408/6 "2008-11-16T22:19:01Z")

</div>

> The users table also has a country\_id column, so inside the User class I also have
> 
> belongs\_to :country

exactly right.

Fred
