In my data model, I have a habtm association between messages and
contact email addresses, with an attribute on the join table
identifying the type of relation (in this case the header, i.e. from,
to, reply-to). I''d like to be able to associate the same address with
a message multiple times, distinguished by the relation, but Rails
doesn''t seem to allow me to do so. For example if I have a message
where the contact has the same address set in ''from'' and
''reply-to'', I
get a duplicate update in the database, because Rails gets the
relation confused (it tries to insert the join record twice with the
relation attribute set to ''reply-to'').
I have code roughly like this:
dbAddress = UnfiledAddress.find_by_email_address(email_address)
      
unless dbAddress
    # check if we have already added this address to this message
    unfiled_addresses.each { |address|
        dbAddress = address.clone if address.email_address == email_address
    }
    # create a new unfiled address if we don''t have one
    dbAddress = UnfiledAddress.new(:display_name  => display_name,
                                   :email_address => email_address,
                                   :most_recent => msg_date) unless dbAddress
end
unfiled_addresses.push_with_attributes(dbAddress, :relation => header)
The address.clone is there after a look at the rails code - it
basically copies the attributes onto the passed record, saves it if
it''s new, and adds it to the list.
Is this actually a reasonable thing to want to do, or is it some
horrible failure of data modelling? Thanks for any and all advice.