Ruby on Rails Friday, July 2, 2010

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'.
--
Posted via http://www.ruby-forum.com/.

--
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