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 8231271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:25:53+00:00 2026-06-07T17:25:53+00:00

I have the following STI setup: class Cage < ActiveRecord::Base has_many :animals end class

  • 0

I have the following STI setup:

class Cage < ActiveRecord::Base
  has_many :animals
end

class Animal < ActiveRecord::Base
  belongs_to :cage
end

class Cat < Animal
  # name = "Tom"
end

class Mouse < Animal
  # name = "Jerry"
end

I would like to do a select on the Animals table that returns the following structure:

+----+-----------------------+
| ID | cat_name | mouse_name |
+----+-----------------------+
| 1  | Tom      | Jerry      | 
+----+-----------------------+

Ideally I would like something like this:

Cage \
  .joins(:animals)
  .select("
    cages.id,
    animals.name as case animals.type 
      when 'Mouse' then mouse_name 
      when 'Cat' then cat_name end
  ") \
  .group('cages.id')

But that doesn’t work so I did this:

Cage \
  .joins(:animals)
  .select("
    cages.id,
    case animals.type when 'Mouse' then animals.name end mouse_name end,
    case animals.type when 'Cat' then animals.name cat_name end
  ") \
  .group('cages.id')

Is there a way to make the first query work and if not is there a better/alternative way to write the second query?

  • 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-06-07T17:25:54+00:00Added an answer on June 7, 2026 at 5:25 pm

    In your first query you’re trying to make two name columns with one CASE expression. That won’t work. If you want two cols in your resultset, you have to select two different things.

    Your second query is closer, but you’re doing a group-by without any aggregate functions in the select, so you could get a random result from each group. I suggest:

    ...
    GROUP_CONCAT(IF(animals.type='Cat',animals.name,NULL)) AS cat_name,
    GROUP_CONCAT(IF(animals.type='Mouse',animals.name,NULL)) AS mouse_name
    ...
    

    If I’m misunderstanding and you actually want both names in one result column, you could do:

    GROUP_CONCAT( 
        CONCAT(animals.type,' name is: ',animals.name) 
        SEPARATOR ', '
    ) AS animal_names
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following: class Series < ActiveRecord::Base has_many :components end class Component <
I have following setup. from django.db import models from django.contrib.auth.models import User class Event(models.Model):
I have following xml file: <os:tax> <os:cat name=abc id=1> <os:subcat name=bcd id=11> <os:t name=def
Have following setup: MainActivity class - extends activity MyLayout class - extends View Prefs
I have the following: Reseller hierarchy, STI, tree structure in the resellers DB table.
I have following class which reads and writes an array of objects from/to a
I have following structure of an HTML document: <body> <div class=main> <div class=left></div> <div
I have following code: public class reader extends Activity { WebView mWebView; String mFilename;
I have following code in html: <div> <div style=float:left;margin:0.5em> <span class=title>Label1</span><br/> <input type=text name=name1
I have following table structure in my DB ID Name -------------------------- ID_1 Name1 ID1

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.