# Activerecord Relationships: Can children access parents?

**URL:** <https://www.ruby-forum.com/t/activerecord-relationships-can-children-access-parents/114260>\
**Category:** Rails\
**Created:** [September 10, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/activerecord-relationships-can-children-access-parents/114260 "2007-09-10T18:10:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [September 10, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/activerecord-relationships-can-children-access-parents/114260/1 "2007-09-10T18:10:02Z")

</div>

I am having trouble when using a child to reference the parent object  
in a relationship, it always claims the parent is nil. I could easily  
be doing something wrong as this is all hazy but I thought I’d ask if  
anyone could see anything obvious:

I have two databases:

rulesets  
id  
name

races  
id  
name  
ruleset\_id

A race specifies it’s ruleset so, a ruleset can have many races, but  
each race will have only 1 ruleset. I have specified a foreign key  
relationship without a constraint (using mysql “foreign key  
(ruleset\_id) references rulesets (id)”) but my version of MySQL  
doesn’t even enforce foreign keys so that shouldn’t help much. In the  
models I have:

class Ruleset \< ActiveRecord::Base  
has\_many :races  
…

class Race \< ActiveRecord::Base  
belongs\_to :rulesets  
…

Then in the controller I have:

@races = Race.find( :all )

And finally in the view:

\<% @races.each do |r| %\>  
…  
\<%= @r.ruleset.name %\>  
…

And it complains that ruleset is not a valid method for races. The  
book I have on Ruby only talks about doing this sort of access in one  
direction (i.e. ruleset.races. not  
races.ruleset.) so I’m not even sure if it’s possible to  
have a child accessing the values of it’s parent. But if it is, what  
am I doing wrong here?

Thanks in advance!!

Chris

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [September 10, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/activerecord-relationships-can-children-access-parents/114260/2 "2007-09-10T18:23:44Z")

</div>

restart your server?

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [September 10, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/activerecord-relationships-can-children-access-parents/114260/3 "2007-09-10T19:11:11Z")

</div>

I just restarted and that did not solve the problem. I’m using  
Webbrick and I’m in development mode, so I’m hoping everything gets  
reloaded each run… but nevertheless, it was worth a shot.

Chris

On Sep 10, 9:23 am, Dustin A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [September 10, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/activerecord-relationships-can-children-access-parents/114260/4 "2007-09-10T19:38:16Z")

</div>

> class Race \< ActiveRecord::Base  
> belongs\_to :rulesets

I think this should be singular:  
belongs\_to :rulesets

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [September 10, 2007, 8:03pm UTC](https://www.ruby-forum.com/t/activerecord-relationships-can-children-access-parents/114260/5 "2007-09-10T20:03:20Z")

</div>

This worked. I changed both references to singular and it works now.  
Funny, my Rails programming text’s examples show using the plural form  
of the DB. I even checked that to make sure it was plural and not  
singular.

Anyway,

belongs\_to :ruleset  
has\_many :races

…did the trick. Thanks so much!

Chris
