I have a User class that is associated with Contact through
has_many :contacts, :through => :user_contacts, :source
=> :contact, :order => 'contacts.last_name ASC'
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?
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|
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.