Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Active Record Associations in Rails Active Record Associations Has Many Through Associations

Eylon Cohen
Eylon Cohen
4,779 Points

belongs_to_and_has_many vs has_many_through

In what cases should I use belongs_to_and_has many? I usually don't know if I will ever need to use this joint table. for space? for convience?

1 Answer

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

Here's a summary of the differences:

https://stackoverflow.com/questions/2780798/has-and-belongs-to-many-vs-has-many-through

One common bit of advice is to always use has_many :through unless you are certain you will never need to add fields to the join table, because it's a bit of a pain to convert from has_and_belongs_to_many to has_many :through. But HABTM is supposedly a bit more efficient, so there are those who say you should use HABTM until you know you need has_many :through. In the end, the choice is yours.