Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Christopher Phillips
Christopher Phillips
10,061 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
STAFF
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.