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

  • Home
  • SEARCH
  • 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 814643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:33:18+00:00 2026-05-15T01:33:18+00:00

I am trying to create a Single Table Inheritance. However, the Controller must be

  • 0

I am trying to create a Single Table Inheritance.

However, the Controller must be able to know which class to find or create. These are based on another class.

For example, ContactEvent with type = Letter needs to grab attributes from a corresponding Model called Letter.

Here’s what I’ve tried to do and hit a snag, labelled below.

I need to be able to dynamically call assign a value of EventClass so that it can be Letter.find(:conditions =>) or Calls.find(:conditions =>) depending on which type the controller is acting on.

  def new
    @contact_event = ContactEvent.new
    @contact_event.type = params[:event_type] # can be letter, call, postcard, email
    @contact_event.event_id = params[:event_id] # that ID to the corresponding Model
    @contact_event.contact_id = params[:contact]

    @EventClass = case
      when @contact_event.type == 'letter' then 'Letter'
      when @contact_event.type == 'call' then 'Call'
      when @contact_event.type == 'email' then 'Email'

SNAG BELOW:

    @event = @EventClass.find(@contact_letter.letter_id) #how do I make @EventClass actually the Class?SNAG

    # substitution of variables into the body of the contact_event
    @event.body.gsub!("{FirstName}", @contact.first_name)
    @event.body.gsub!("{Company}", @contact.company_name) 
    @evebt.body.gsub!("{Colleagues}", @colleagues.to_sentence)

    @contact_event.body = @event.body
    @contact_event.status = "sent"

  end
  • 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-15T01:33:19+00:00Added an answer on May 15, 2026 at 1:33 am
    @event_class = case @contact_event.type
      when 'letter' then Letter
      when 'email'  then Email
      when 'call'   then Call
    end
    

    You have to assign the class itself, not a string. You can then just call @event_class.find(whatever). Some things to note:

    Single table inheritance is supported, and handled automatically by Rails. You can have Letter, Email and Call inherit from ContactEvent. Read more at Rails API (section Single Table Inheritance)

    You can convert your string directly by calling @contact_event_type.classify.constantize (this turns “call” into Call, for example).

    Also, try to name your variables according to ruby conventions (@event_type instead of @EventType), and check your use of the case statement.

    Hope it’s useful 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 472k
  • Answers 472k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your "SQL": SELECT reservations->reservation['seat'] FROM performances->performance['id=2'] can be reformulated as:… May 16, 2026 at 3:38 am
  • Editorial Team
    Editorial Team added an answer There's always ./configure LDFLAGS=-L$HOME/opt/lib CPPFLAGS=-I$HOME/opt/include. But that method globally uses… May 16, 2026 at 3:38 am
  • Editorial Team
    Editorial Team added an answer You need to add a <label> element for the radio… May 16, 2026 at 3:37 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.