# Relationships in Rails

**URL:** <https://www.ruby-forum.com/t/relationships-in-rails/158637>\
**Category:** Rails\
**Created:** [January 26, 2009, 10:18am UTC](https://www.ruby-forum.com/t/relationships-in-rails/158637 "2009-01-26T10:18:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [January 26, 2009, 10:18am UTC](https://www.ruby-forum.com/t/relationships-in-rails/158637/1 "2009-01-26T10:18:27Z")

</div>

I have 2 tables:

people (id, name)  
children (id, parents\_id, child\_id)

parents\_id = people.id and child\_id = people\_id also

So how I make a relationships in Rails?

And how can all search:

Children.find(:all, :conditions =\> ["parents.name LIKE ‘Clin%’ ??? Or  
how???

And what if I want to add new child. Parents name is in people table,  
but childs not, so how I do?

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [January 26, 2009, 10:51am UTC](https://www.ruby-forum.com/t/relationships-in-rails/158637/2 "2009-01-26T10:51:44Z")

</div>

Sounds like a many to many relationship, in which case you need to  
declare has\_and\_belongs\_to\_many on both people and children. The  
relationship then works through a join table. If you did this, you’d  
need to re think your model a little and your find and add problems  
would be different.  
Martin

On Jan 26, 9:18 am, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [January 26, 2009, 12:50pm UTC](https://www.ruby-forum.com/t/relationships-in-rails/158637/3 "2009-01-26T12:50:06Z")

</div>

I think that the best solution to your problem is to use acts\_as\_tree

> **[GitHub - rails/acts\_as\_tree: NOTICE: official repository moved to...](https://github.com/rails/acts_as_tree/tree/master)**
>
> master

Martin H.  
escribió:\> Sounds like a many to many relationship, in which case you need to
