Sync :through association

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

class User
has_many :user_contacts
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|

Do stuff with contact

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|
puts uc.contact.last_name
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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs