Tutorial :How can I determine the Model by extracting the name of another Model?



Question:

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?


Solution:1

You can do it with a regexp

event.class.name.gsub(/Contact/, '').downcase + '_id'  

But certainly I'm more partisan of adding a method contact_field_id into all these classes, and just make model_name = event.contact_field_id. Not only is cleaner, but more scalable.

EDIT:

Seems you are trying to put logic into the controller to find the right instance to get from the database. This shouldn't correspond to the controller, but rather to the model. Following what I've proposed before, I'd do:

class ContactEmail    def contact(id)      Email.find_by_email_id(id)    end  end    class ContactLetter    def contact(id)      Letter.find_by_letter_id(id)    end  end  

So you have not to worry about finding the right class, just call @event.contact(id). Perhaps this is what you're looking for


Solution:2

You can do what EricBoersma suggests, and have each class implement a common interface to get the id. Something like this:

class ContactEmail    def contact_id      email_id    end  end    class ContactLetter    def contact_id      letter_id    end  end    class ContactCall    def contact_id      call_id    end  end  

Then just use event.contact_id wherever you need to access the proper id.


Solution:3

I'd suggest having all of these event classes implement an Event interface which has a getID() method. Each of the subclasses should implement that method to return their particular ID.

However, I'm not a Ruby expert so I don't know if that's possible.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »