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

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