On 1 July 2010 23:48, khagimoto <kumi.hagimoto@gmail.com> 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"
> ...
>
> 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?
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
>
> Thanks for any insights!
>
> --
> 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 rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment