Forum: Ruby on Rails Question about has_many/belongs_to

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
E293f33f13ca0525764e4066a127cb2f?d=identicon&s=25 Sven Magnus (Guest)
on 2007-03-09 10:28
(Received via mailing list)

In the firm I work, we're disabling table name pluralization because
this complies to our standards.

So, suppose I have a table client and a table contact_person (which
has a client_id). I also have the models Client and ContactPerson for

In my Client class, I would have :

has_many :contact_person

and in the ContactPerson class I would have

belongs_to :client

Is this correct? Or do I need to pluralize has_many :contact_person?

Also, how do I get the collection of the ContactPersons associated to
a specific Client object? I tried @client.contactPersons and
@client.contactPeople, but neither seems to work.

I know this is all basic stuff, but I'm still trying to get my head
around ruby/rails.

Thanks for your time,

Sven Magnus
8f23570fbde6277118fcd088a675f61a?d=identicon&s=25 Joshua Kahn (jkahn)
on 2007-03-09 16:26
In your Client class, you should have

  has_many :contact_people

Be sure to implicitly specify the table names in the Client and
ContactPerson models using set_table_name; otherwise, Rails will look
for "clients" and "contact_people" in your database.

If you already have a collection of contact_people (an array), you could

  client.contact_people = my_collection

Rails tends to use underscores instead of camel case. You might want to
take a look at the associations documentation in the API - there are
good explanations of these concepts there.
This topic is locked and can not be replied to.