# T.references usage

**URL:** <https://www.ruby-forum.com/t/t-references-usage/180547>\
**Category:** Rails\
**Created:** [December 21, 2009, 3:44pm UTC](https://www.ruby-forum.com/t/t-references-usage/180547 "2009-12-21T15:44:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [December 21, 2009, 3:44pm UTC](https://www.ruby-forum.com/t/t-references-usage/180547/1 "2009-12-21T15:44:14Z")

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

# – create\_table(:subscribers) → 0.0064s == CreateSubscribers: migrated (0.0068s)

# == PopulateSubscribers: migrating

# == PopulateSubscribers: migrated (0.0443s)

# == CreateSubscriptions: migrating

– create\_table(:subscriptions)  
– subscribers()  
rake aborted!  
An error has occurred, all later migrations canceled:

undefined method `subscribers’ for  
#ActiveRecord::ConnectionAdapters::MysqlAdapter:0xb74e78b8  
/home/thufir/projects/rss2mysql/rakefile:9  
(See full trace by running task with --trace)  
thufir@ARRAKIS:~/projects/rss2mysql$  
thufir@ARRAKIS:~/projects/rss2mysql$ cat db/migrate/00  
0010\_create\_subscribers.rb 0030\_create\_subscriptions.rb  
0050\_create\_items.rb  
0020\_populate\_subscribers.rb 0040\_populate\_subscriptions.rb  
0060\_create\_pages.rb  
thufir@ARRAKIS:~/projects/rss2mysql$ cat db/  
migrate/0040\_populate\_subscriptions.rb  
class PopulateSubscriptions \< ActiveRecord::Migration

class Subscription \< ActiveRecord::Base  
end

def self.up  
s1 = Subscription.create(:subscription =\> ‘[http://groups.google.ca/](http://groups.google.ca/)  
group/ruby-talk-google/feed/rss\_v2\_0\_msgs.xml’,:subscriber\_id =\> 1)  
s2 = Subscription.create(:subscription =\> ‘[http://www.slashdot.org/](http://www.slashdot.org/)  
index.rss’,:subscriber\_id =\> 2)  
end

def self.down  
end

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

def self.down  
drop\_table :subscriptions  
end  
end

thufir@ARRAKIS:~/projects/rss2mysql$

is there an error with migration, or in how it’s used?

thanks,

Thufir

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [December 21, 2009, 8:21pm UTC](https://www.ruby-forum.com/t/t-references-usage/180547/2 "2009-12-21T20:21:41Z")

</div>

On Mon, Dec 21, 2009 at 9:40 AM, Thufir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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”:

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

instead of

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

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

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [December 21, 2009, 8:40pm UTC](https://www.ruby-forum.com/t/t-references-usage/180547/3 "2009-12-21T20:40:25Z")

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

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
