# Has\_many and belongs\_to in rails 2.0.2 (child.Parent) is not working

**URL:** <https://www.ruby-forum.com/t/has-many-and-belongs-to-in-rails-2-0-2-child-parent-is-not-working/129912>\
**Category:** Rails\
**Created:** [February 22, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-in-rails-2-0-2-child-parent-is-not-working/129912 "2008-02-22T16:53:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hhvm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@hhvm](https://www.ruby-forum.com/u/hhvm)\
**Post date:** [February 22, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-in-rails-2-0-2-child-parent-is-not-working/129912/1 "2008-02-22T16:53:34Z")

</div>

Hi All

I have 2 models working well in rails 1.2.6 :

class TypeService \< ActiveRecord::Base  
has\_many :Services  
end

class Service \< ActiveRecord::Base  
belongs\_to :TypeService  
end

I was able to get the name of the service with Rails 1.2.3:

s = Service.find(1)  
name\_type\_service = s.TypeService.name \<\<-- Now with rails 2.0.2  
that doesn’t work

With rails 2.0.2

s = Service.find(1)  
s.TypeService.name  
nil \<\<-- TypeService is nil with rails 2.0.2

Does any one know why that “child.parent” is not working with rails  
2.0.2 anymore?

Thank you very much for your help

---

<div class="post-metadata">

**Author:** ![hhvm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@hhvm](https://www.ruby-forum.com/u/hhvm)\
**Post date:** [February 22, 2008, 4:58pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-in-rails-2-0-2-child-parent-is-not-working/129912/2 "2008-02-22T16:58:51Z")

</div>

On 22 Feb 2008, at 15:53, hhvm wrote:

> belongs\_to :TypeService  
> end

The expected names would be has\_many :services and  
belongs\_to :type\_service. It’s possible that 2.0 is enforcing this  
more strictly.

Fred.

---

<div class="post-metadata">

**Author:** ![hhvm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@hhvm](https://www.ruby-forum.com/u/hhvm)\
**Post date:** [February 22, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-in-rails-2-0-2-child-parent-is-not-working/129912/3 "2008-02-22T17:35:01Z")

</div>

Thank you very much but I just changed that, and No, that is still  
not working.

I’m not sure why I’m not able to get the Parent class in rails 1.2.3  
that was working well.

parent = child.Parent \<\<-- that was i  
nil \<\<-- nil with rails 2.0.2

On Feb 22, 10:58 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![hhvm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@hhvm](https://www.ruby-forum.com/u/hhvm)\
**Post date:** [February 22, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-in-rails-2-0-2-child-parent-is-not-working/129912/4 "2008-02-22T20:05:09Z")

</div>

hhvm wrote:

> parent = child.Parent \<\<-- that was i  
> nil \<\<-- nil with rails 2.0.2

It depends on the definition of your services table. The standard way  
is for the services table to contain a column called type\_service\_id and  
then use:

class TypeService \< ActiveRecord::Base  
has\_many :services  
end

class Service \< ActiveRecord::Base  
belongs\_to :type\_service  
end

s = Service.find(1)  
name\_type\_service = s.type\_service.name

The arguments to “has\_many” and “belongs\_to” become methods in the class  
which return the connected object(s) from the other class. The  
“belongs\_to” method returns a single object and the “has\_many” method  
returns an array of objects, so similarly:

t = TypeService.find(1)  
services\_for\_t = t.services

Here services\_for\_t is now a (possibly empty) array of Service objects.

If the column in your services table is called something else, then you  
need to specify it as the foreign key in the relation, so if this column  
is typeservice\_id, for example, use:

class TypeService \< ActiveRecord::Base  
has\_many :services, :foreign\_key =\> :typeservice\_id  
end

class Service \< ActiveRecord::Base  
belongs\_to :type\_service, :foreign\_key =\> :typeservice\_id  
end

---

<div class="post-metadata">

**Author:** ![hhvm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@hhvm](https://www.ruby-forum.com/u/hhvm)\
**Post date:** [February 22, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-in-rails-2-0-2-child-parent-is-not-working/129912/5 "2008-02-22T21:41:38Z")

</div>

Thank you very much,

I got it I was using TypeService  
like :

belongs\_to :TypeService

I change it to

belongs\_to :type\_service

now that is working,

Thank you Mark for your help.

Regards

On Feb 22, 2:05 pm, Mark B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
