# t.references usage

**URL:** <https://discuss.rubyonrails.org/t/t-references-usage/48119>\
**Category:** rubyonrails-talk\
**Created:** [December 21, 2009, 2:40pm UTC](https://discuss.rubyonrails.org/t/t-references-usage/48119 "2009-12-21T14:40:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@Thufir](https://discuss.rubyonrails.org/u/Thufir)\
**Post date:** [December 21, 2009, 2:40pm UTC](https://discuss.rubyonrails.org/t/t-references-usage/48119/1 "2009-12-21T14:40:39Z")

</div>

While I'm not currently using the whole rails environment, I may utilize the framework in the future. In the meantime, I'm not clear on the syntax for establishing a foreign key relationship for an active record association using "t.references" or "t.belongs\_to":

thufir@ARRAKIS:~/projects/rss2mysql$ thufir@ARRAKIS:~/projects/rss2mysql$ rake migrate (in /home/thufir/projects/rss2mysql) == CreateSubscribers: migrating

---

<div class="post-metadata">

**Author:** ![Patrick\_Doyle](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Patrick\_Doyle](https://discuss.rubyonrails.org/u/Patrick_Doyle)\
**Post date:** [December 21, 2009, 7:16pm UTC](https://discuss.rubyonrails.org/t/t-references-usage/48119/2 "2009-12-21T19:16:51Z")

</div>

3 things came to my mind as I read your post...

1) Upgrade to a later version of Rails. You don't say what version you are using, but the convention of migrations named NNNN\_some\_name changed to YYYYMMDDHHMMSS\_some\_name about a year(ish) ago when I first started learning about Rails.

2) I wonder if you have a pluralization problem and if yuo should change your CreateSubscriptions class to use

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.references subscriber

instead of

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.references subscribers. In general, I have learned that a "thing" model "has\_many :somethings" (note the plural) while the Something model would "belongs\_to :thing" (note the singular).

3) I also wonder why you commented out the creation of the primary key for your Subscriptions table, although that is not likely to be your problem.

--wpd

---

<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:** [December 21, 2009, 7:39pm UTC](https://discuss.rubyonrails.org/t/t-references-usage/48119/3 "2009-12-21T19:39:01Z")

</div>

> thufir@ARRAKIS:~/projects/rss2mysql$
> 
> is there an error with migration, or in how it's used?

well much like you write t.column :subscriber\_id and not t.column subscriber\_id, you need to write t.references :subscribers

Fred

---

<div class="post-metadata">

**Author:** ![Thufir](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@Thufir](https://discuss.rubyonrails.org/u/Thufir)\
**Post date:** [December 24, 2009, 11:59am UTC](https://discuss.rubyonrails.org/t/t-references-usage/48119/4 "2009-12-24T11:59:24Z")

</div>

I went with t.references :subscriber as that worked better with my script:

thufir@ARRAKIS:~/projects/rss2mysql$ thufir@ARRAKIS:~/projects/rss2mysql$ cat db/ migrate/0030\_create\_subscriptions.rb class CreateSubscriptions \< ActiveRecord::Migration &nbsp;&nbsp;def self.up &nbsp;&nbsp;&nbsp;&nbsp;create\_table :subscriptions do |t| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#t.column :subscriber\_id, :integer #, :null =\> false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.references :subscriber &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :subscription, :string &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;def self.down &nbsp;&nbsp;&nbsp;&nbsp;drop\_table :subscriptions &nbsp;&nbsp;end end

thufir@ARRAKIS:~/projects/rss2mysql$

which is correct, subscriber or subscribers?

-Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@Thufir](https://discuss.rubyonrails.org/u/Thufir)\
**Post date:** [December 24, 2009, 12:02pm UTC](https://discuss.rubyonrails.org/t/t-references-usage/48119/5 "2009-12-24T12:02:45Z")

</div>

> > While I'm not currently using the whole rails environment, I may utilize the framework in the future.

[...]

> 1) Upgrade to a later version of Rails.

I may use the framework at a later point, but for now am just using Active Record.

> 2) I wonder if you have a pluralization problem and if yuo should change your CreateSubscriptions class to use
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.references subscriber
> 
> instead of
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.references subscribers. In general, I have learned that a "thing" model "has\_many :somethings" (note the plural) while the Something model would "belongs\_to :thing" (note the singular).

Yes, I seem to be having this problem, but I \*seem\* to have worked it out (see seperate post).

> 3) I also wonder why you commented out the creation of the primary key for your Subscriptions table, although that is not likely to be your problem.

Oh, only because rather than create it manually, I wanted to use this "references" feature.

-Thufir

---

<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:** [December 24, 2009, 1:49pm UTC](https://discuss.rubyonrails.org/t/t-references-usage/48119/6 "2009-12-24T13:49:49Z")

</div>

subscriber (that was just a typo on my behalf - the main thing is that you need to be passing a string or symbol)

Fred
