# Using Associations vs Child\_Model.find

**URL:** <https://www.ruby-forum.com/t/using-associations-vs-child-model-find/190989>\
**Category:** Rails\
**Created:** [July 2, 2010, 12:49am UTC](https://www.ruby-forum.com/t/using-associations-vs-child-model-find/190989 "2010-07-02T00:49:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![khagimoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@khagimoto](https://www.ruby-forum.com/u/khagimoto)\
**Post date:** [July 2, 2010, 12:49am UTC](https://www.ruby-forum.com/t/using-associations-vs-child-model-find/190989/1 "2010-07-02T00:49:22Z")

</div>

I have a model (Parent) that has a has\_many association with another  
model (Child). I read that there are performance issues (relating to  
GC) with too many associations. I was about to add more has\_many  
associations to the parent model to get child objects ordered by  
different columns like so:

(Inside Parent model)  
…  
has\_many :child, :through =\> :childx, :order =\> “category\_1, date\_1  
desc”  
has\_many :child1, :through =\> :childx, :source =\> :child, :order =\>  
“updated\_at desc”  
has\_many :child2, :through =\> :childx, :source =\> :child, :order =\>  
“date\_1 desc”  
has\_many :child3, :through =\> :childx, :source =\> :child, :order =\>  
“name\_1”  
…

Is this going to make ruby hold on to this parent object and 4 child  
object and use up a lot of memory? (Does the parent model get all the  
child records child, child1, child2 and child3 as soon as Parent.find  
is called? Or are they only really queried when I access, say,  
parent.child1?)

Is it better to just do a Child.find with the parent\_id inside the  
controller? Or would it use up as much memory as soon as I do  
Child.find?

Thanks for any insights!

---

<div class="post-metadata">

**Author:** ![khagimoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@khagimoto](https://www.ruby-forum.com/u/khagimoto)\
**Post date:** [July 2, 2010, 5:29pm UTC](https://www.ruby-forum.com/t/using-associations-vs-child-model-find/190989/2 "2010-07-02T17:29:44Z")

</div>

Thanks, Ar Chron - I’m afraid I don’t really understand what you’re  
suggesting that I do. (sorry…) I already have a cross-reference  
table (childx) between the parent and the child that connects them.  
In my model, parent has many child, but child can have only one  
parent.

The only reason I am defining the child, child1, child2 and child3  
inside the parent model is to get a list of child records that belongs  
to a parent sorted by different columns in the child table. (I  
thought it was an easy and efficient way to get at the record … :-  
but maybe this is not a good idea?) And my question was if this is  
better or worse memory-wise than doing something like this:

```
         # method defined in the Child model
        Child.find_by_parent_id(some_parent_id, sort_order)

```

inside the controller each time I needed a child record pulled sorted  
by a different column. (So no child, child1, child2 or child3 defined  
in Parent.)

Thanks again - I’ll try to read more to understand this. Maybe it’ll  
lead to what you were trying to tell me in the first place. 🙂

---

<div class="post-metadata">

**Author:** ![khagimoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@khagimoto](https://www.ruby-forum.com/u/khagimoto)\
**Post date:** [July 2, 2010, 5:56pm UTC](https://www.ruby-forum.com/t/using-associations-vs-child-model-find/190989/3 "2010-07-02T17:56:40Z")

</div>

On 1 July 2010 23:48, khagimoto [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_many :child1, :through =\> :childx, :source =\> :child, :order =\>  
> is called? Or are they only really queried when I access, say,  
> parent.child1?)
> 
> Is it better to just do a Child.find with the parent\_id inside the  
> controller? Or would it use up as much memory as soon as I do  
> Child.find?

Provide some named scopes in Child to do the sorting then you can do  
parent.children.sorted\_by\_desc  
parent.children.sorted\_by\_name  
or whatever (you might think of better names). This should do the  
sort in the db query so there should be no efficiency issues, provided  
you have indexes on the sorted columns of course.

Colin

---

<div class="post-metadata">

**Author:** ![khagimoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@khagimoto](https://www.ruby-forum.com/u/khagimoto)\
**Post date:** [July 5, 2010, 11:56pm UTC](https://www.ruby-forum.com/t/using-associations-vs-child-model-find/190989/4 "2010-07-05T23:56:55Z")

</div>

Hi Colin,  
This was exactly what I was after. I have to do some performance  
testing, but it already seems faster with named\_scope. 🙂  
Thanks very much!  
Kumi

---

<div class="post-metadata">

**Author:** ![khagimoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ee59a6/32.png) [@khagimoto](https://www.ruby-forum.com/u/khagimoto)\
**Post date:** [July 2, 2010, 3:24pm UTC](https://www.ruby-forum.com/t/using-associations-vs-child-model-find/190989/5 "2010-07-02T15:24:10Z")

</div>

khagimoto wrote:

> I have a model (Parent) that has a has\_many association with another  
> model (Child). I read that there are performance issues (relating to  
> GC) with too many associations. I was about to add more has\_many  
> associations to the parent model to get child objects ordered by  
> different columns like so:
> 
> (Inside Parent model)  
> …  
> has\_many :child, :through =\> :childx, :order =\> “category\_1, date\_1  
> desc”  
> has\_many :child1, :through =\> :childx, :source =\> :child, :order =\>  
> “updated\_at desc”  
> has\_many :child2, :through =\> :childx, :source =\> :child, :order =\>  
> “date\_1 desc”  
> has\_many :child3, :through =\> :childx, :source =\> :child, :order =\>  
> “name\_1”  
> …

This design cries out for refactoring… as soon as I see something1,  
something2, something3 when the series is essentially infinite (though I  
wouldn’t want to feed or clothe them all), alarm bells start ringing.

Consider a ‘relations’ model that takes a person id, a different person  
id, and a relation (child, parent, grandparent, spouse, ex-spouse, etc,  
etc), which maintains that “person1 is a relation of person2”.

Extra points if you implement the relation model in a bi-directional  
order (each relation needs to know its inverse), whereby creating the  
tuple ‘person1, parent, person2’ automatically creates ‘person2, child,  
person1’.
