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

Christopher Phillips
Christopher Phillips
10,054 Points

Confused with naming conventions of symbols (singular vs plural)

In the video, you've shown three tables all with pluralized names: subscribers, subscribers_magazines, and magazines.

The model name you created (Subscription) is a singular version of the "through" table name.

When you were updating the model classes for all three, you used the singular version for the belongs_to association but then the pluralized form for the has_many associations.

I'm trying to prevent myself from getting confused over when to use the singular or plural variety because with the belongs_to association, you specified "subscriber" and "magazine", neither of which have a specific table name associated with the respective symbol.

With excel, if I don't specify the exact cell in reference, the value is incorrect. How does Rails know which table to use if we give it a symbol that hasn't been defined?

Thank you

1 Answer

Jay McGavren
Jay McGavren
Treehouse Teacher

I agree that it's confusing. The general mnemonic is that in many places, Rails tries to let you write your code in what looks sort of like English. You wouldn't write "a magazine has many subscriber" or "a subscriber belongs to a magazines", so Rails lets you (or expects you to) write has_many :subscribers and belongs_to :magazine.

To support this, it uses helper functions like pluralize and singularize to convert from one form to the other where needed.

Here's a handy reference for which forms you should use where.