Eric
Eric

Reputation: 31

Create join table using migrations or models?

As a Ruby on Rails newbie, I'm going through the Rails Guides and tonight is Active Record Migrations.

After finishing the section on Join Tables (http://guides.rubyonrails.org/active_record_migrations.html#creating-a-join-table), I'm left with the impression that using create_join_table is preferred (and simpler) than creating the Join Table via rails generate model.

Is this a correct assumption? Are there nuances I should be aware of?

Upvotes: 3

Views: 7781

Answers (2)

Richard Peck
Richard Peck

Reputation: 76774

As the docs mention...

Migration method create_join_table creates a HABTM join table

--

When you create a has_and_belongs_to_many relationship, you only need a join table (no model).

has_and_belongs_to_many join tables are different from has_many :through as they don't have an id (primary_key); has_many :through are meant to represent an interim model, so each "record" should have a unique identifier.

Thus, your question about whether it's better to create a join table through rails g model is false. has_and_belongs_to_many has no binding model. If you were using has_many :through, you'd be able to use the rails g model no problem.

Upvotes: 2

Julien Negrotto
Julien Negrotto

Reputation: 458

Using the example in the guides of categories and products:

A join table works transparently. You only work with the two existing models (Category and Product), and the join table exists only to enable the HABTM relationship between them, so you can call category.products, or product.categories, and things just work.

Generating a model, on the other hand, would only be necessary if you need to work with that association as a distinct thing in your application (e.g. if you need to do things with a Categorization directly).

Contrast the Rails Guides description of a has_and_belongs_to_many association (read more):

A has_and_belongs_to_many association creates a direct many-to-many connection with another model, with no intervening model. For example, if your application includes assemblies and parts, with each assembly having many parts and each part appearing in many assemblies, you could declare the models this way: enter image description here

with that of a has_many :through association (read more):

A has_many :through association is often used to set up a many-to-many connection with another model. This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding through a third model. For example, consider a medical practice where patients make appointments to see physicians. The relevant association declarations could look like this: enter image description here

So, yes, you're correct that create_join_table would be simpler than creating a model for the association. You can also see this answer for another explanation of the difference between these approaches.

Upvotes: 8

Related Questions