# Has\_and\_belongs\_to\_many with one-way "many"

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-with-one-way-many/68504>\
**Category:** Rails\
**Created:** [September 13, 2006, 7:32am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-with-one-way-many/68504 "2006-09-13T07:32:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 13, 2006, 7:32am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-with-one-way-many/68504/1 "2006-09-13T07:32:26Z")

</div>

Hi-

My model is such that I have Users who “own” 1 or more Contacts. My  
schema is like this (abriged):

CREATE TABLE users (  
id int primary key auto\_increment,  
first\_namename varchar(40)  
…more columns…  
);

CREATE TABLE contacts(  
id int primary key auto\_increment,  
…more columns…  
);

CREATE TABLE user\_contacts (  
user\_id int not null,  
contact\_id int null,  
CONSTRAINT fk\_uc\_user FOREIGN KEY (user\_id) REFERENCES user(id),  
CONSTRAINT fk\_uc\_contact FOREIGN KEY (contact\_id) REFERENCES  
contacts(id),  
);

Now, a user a can have multiple contacts, but from the other direction  
a single contact is “owned” by only 1 user. Yes, I could put a  
“user\_id” column in the Contacts table and not have the linking table  
there at all, but this seems like bad design.

Currently, my model is:

class Contact \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :user, :join\_table =\> “user\_contacts”  
end

and

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :contact, :join\_table =\> “user\_contacts”  
end

and this works. However, when back tracking via a contact, its an array  
for “user”:

> > c = Contact.find(4)  
> > =\> #\<Contact:0xb7431200 @attributes={“created\_on”=\>“2006-09-12  
> > 22:08:24”, “dob”=\>“2006-09-12”, “id”=\>“4”, “first\_name”=\>“Foo”,  
> > “last\_name”=\>“Bar”}\>
> 
> > c.user  
> > =\> [#\<User:0xb794d164 @attributes={“created\_on”=\>“2006-09-11 23:58:28”,  
> > “contact\_id”=\>“4”, “id”=\>“1”, “user\_id”=\>“1”, “first\_name”=\>“Blah”,  
> > “password”=\>“xxxx”, “last\_name”=\>“Blubber”,  
> > “email”=\>“removed\_email\_address@domain.invalid”}\>]
> 
> > c.user[0].first\_name  
> > =\> “Blah”
> 
> >

Notice the reference to the 0th array element. Now what I would like to  
do is use my schema but have it enforce contact(many) to user(one)  
relationships without loading an array of objects going from contact to  
user. I think a “has\_many :contact” in User is appropriate, but from  
the Contact side I dont see which association works, “belong\_to” doesnt  
take any options that support going through a secondary join table.

What are my options?

Thanks in advance  
/Cody

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 13, 2006, 7:52am UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-with-one-way-many/68504/2 "2006-09-13T07:52:28Z")

</div>

On 9/12/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My model is such that I have Users who “own” 1 or more Contacts. My  
> schema is like this (abriged):
> 
> …
> 
> Now, a user a can have multiple contacts, but from the other direction  
> a single contact is “owned” by only 1 user. Yes, I could put a  
> “user\_id” column in the Contacts table and not have the linking table  
> there at all, but this seems like bad design.

Revisit this assumption and you’ll be pleased with the results.  
Contact belongs\_to :user  
User has\_many :contacts

jeremy
