I have several models: ContactEmail, ContactLetter, ContactCall
My controller doesn't know what is being passed into it. It only knows it as an event (so event can be specific instance of ContactEmail, ContactLetter, ContactCall)
Each of these has a different attribute. ContactEmail has email_id; ContactLetter has letter_id, ContactCall has call_id.
So here is my question:
For any given instance of the event, how do I access the appropriate Model and associated ID?
In other words, how can I extract from event the Model Name (Email, Letter), and then assign the right id (email_id, letter_id) to then find the right record?
I started with the following:
model = event.class # this will show ContactPostalcard
puts event.send('contact_id')
puts model
short_model_name = model.name.sub('Contact','')
puts short_model_name
short_model = short_model_name.class
key_name = short_model_name.foreign_key
puts key_name
asset_id = event.send("#{key_name}")
puts asset_id
asset = short_model_name.send(find.send(asset_id))
What I want to do is find the associated asset (Email, Letter) that is implicit in the event, and get the specific asset referenced by the email_id (letter_id, voicemail_id) that is in each ContactEmail.
For example: asset = Email.find(ContactEmail.email_id) would give me the asset. So I would need to be able to dynamically create 'Email', email_id. I have the pieces conceptually, but it doesn't work :(.
Awarded to Chubas, but wanted to validate if this is the right direction (some additional information/clarification):
ContactEmail.rb
class ContactEmail
def get_asset
email_id = self.email_id
asset = Email.find_by_email(email_id)
return asset
end
end
Does this look right?