Re: [Rails] Best Models Associations suggestions
There are two things I like about has_many :through. First, it causes me to think about and try to name the relationship. Second, it treats the join model as a first class entity, formalizing the relationship - so i can say book.catalog_entries as well as book.subjects.
Form me, has_and_belongs_to_many obscures rather than reveals relationships so avoided it.
My preference does not define general practice. So let's look at one authority. From The Rails 3 Way, section 7.5.1.
That, too, is an opinion. But one probably more broadly founded than mine.Before proceeding with this section, I must clear my conscience by stating that has_and_belongs_to_many is practically obsolete in the minds of many Rails developers, including the authors of this book. Use has_many :through instead and your life should be a lot easier. The section is preserved in this edition almost exactly as it appeared in the first, because it contains good techniques that enlighten the reader about nuances of Active Record behavior.
As to a 'need' for CatalogEntry.... You need a join model. HABTM uses a join model. It just elides the fact.
Is the devil's advocate satisfied?
On 06/22/2011 01:31 PM, Tim Shaffer wrote:
On Wednesday, June 22, 2011 1:21:06 PM UTC-4, gallivan wrote:I like has_many through with a CatalogEntry join model.--
Devil's advocate here... what do you gain in this example by using a has_many :through instead of has_and_belongs_to_many? Is one ever really going to neeed to access a CatalogEntry instance?
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/AGK7uvDgf-4J.
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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home