# Sync :through association

**URL:** <https://discuss.rubyonrails.org/t/sync-through-association/19562>\
**Category:** rubyonrails-talk\
**Created:** [October 22, 2007, 11:54pm UTC](https://discuss.rubyonrails.org/t/sync-through-association/19562 "2007-10-22T23:54:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gobigdave](https://avatars.discourse-cdn.com/v4/letter/g/958977/32.png) [@gobigdave](https://discuss.rubyonrails.org/u/gobigdave)\
**Post date:** [October 22, 2007, 11:54pm UTC](https://discuss.rubyonrails.org/t/sync-through-association/19562/1 "2007-10-22T23:54:10Z")

</div>

I have a User class that is associated with Contact through UserContact.

class User &nbsp;&nbsp;has\_many :user\_contacts &nbsp;&nbsp;has\_many :contacts, :through =\> :user\_contacts, :source =\> :contact, :order =\> 'contacts.last\_name ASC' end

UserContact defines a many-to-many relationship with attributes. The problem I have is how to best access the attributes in UserContact without constantly accessing the database. For example:

contacts = user.contacts contacts.each do |contact| &nbsp;&nbsp;# Do stuff with contact &nbsp;&nbsp;# How do I access the UserContact instance that through used to access the contact? end

I could do

uc = user.user\_contacts.find\_by\_contact\_id(contact.id)

but that results in another database call for each contact.

I suppose I could do something like: user\_contacts = user.user\_contacts.find(:all, :include =\> :contact, :order =\> 'contacts.last\_name ASC') user\_contacts.each do |uc| &nbsp;&nbsp;puts uc.contact.last\_name &nbsp;&nbsp;puts uc.attribute\_name end

I was hoping there might be another way to work :through backwards from the target. The above will work, but it is not as clean as the :through relationship.
