Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 972989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:12:45+00:00 2026-05-16T03:12:45+00:00

I have several models: ContactEmail, ContactLetter, ContactCall My controller doesn’t know what is being

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:12:46+00:00Added an answer on May 16, 2026 at 3:12 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several similar Models (ContactEmail, ContactLetter, ContactPostalcard). Each instance (record) in, say, ContactEmail,
I would like to have functionality shared by several models to be abstracted into
I have several models grouped in a module like this: #/app/models/blobs/small_text.rb class Blobs::SmallText <
I have several models that belong_to :status . Statuses are: Active Hidden Banned I
In my project, I have several models that use a number of scopes that
I have several doctrine models that have relationships to other models. Let's call those
If you have several view models on one page, how do you ensure that
I have several 'lookup' fields in my models where I don't want nulls, but
I have several small classes that are in a single file in /app/models, similar
I have several models: Email Letter Call All three belong to a model Campaign.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.