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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:32:05+00:00 2026-06-07T18:32:05+00:00

I have a problem where rails is adding unnecessary (I think) clauses to my

  • 0

I have a problem where rails is adding unnecessary (I think) clauses to my belongs_to association – restricting on type even though my association is using a foreign key.

The STI setup looks like this:

class Foo < ActiveRecord::Base
  belongs_to :apple
end

class Apple < Fruit
  has_many :foos
end

So Foo has an apple_id column which links to the primary key column ID in fruits, and Apple is STI under Fruit. I’m happy with all of this.

Now:

> Foo.joins(:apple).to_sql

SELECT "foos".* FROM "foos" INNER JOIN "fruits" ON "fruits"."id" = 
"foos"."apple_id" AND "fruits"."type" IN ('Apple')

Why is rails adding AND "fruits"."type" IN ('Apple')? It’s a belongs_to using a primary key of the fruits table, so the type part seems redundant. Can I stop rails from adding that part to the lookups and just get this:

SELECT "foos".* FROM "foos" INNER JOIN "fruits" ON "fruits"."id" = 
"foos"."apple_id"

I know I could do belongs_to :apple, :class_name => "Fruit" in Foo, but I want the objects to auto-become Apples when they’re returned.

In case someone questions my motives… I want to do this because the type clause is messing up the query plan postgres chooses when I’m doing a query through fruits to other tables (yes, I have an index on type and even tried a multi-column one on [type,id]). That’s a little complicated / irrelevant to describe fully here.

  • 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-07T18:32:08+00:00Added an answer on June 7, 2026 at 6:32 pm

    Basically AM is adding the (‘Apple’) part because in order to respect the inheritance chain.

    For example if you’d have

    class GreenApple < Apple
    end
    

    You’ll get.

    Foo.joins(:apple).to_sql
    SELECT “foos”.* FROM “foos” INNER JOIN “fruits” ON “fruits”.”id” = “foos”.”apple_id” AND “fruits”.”type” IN (‘Apple’, ‘GreenApple’)

    If for whatever reason you end up having on your fruits tabel a fruit with a referenced pk but with a different type you could end up instantiating a different kind of object without knowing it and after that things can go wrong without getting an exception.

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

Sidebar

Related Questions

I have such problem: I'm using rails, devise gem and jQuery validation plugin. I
I am using rails 2.3 and have a problem. I have remote form, and
I have a basic problem with a has_one and belongs_to association. I have two
I have a problem with relations while using sqlite3 on rails. First i build
I have some kind of problem when adding models to my Ruby on Rails
Ok so I have a problem with rails and rendering partials. I have a
I have problem with Spork (v 1.0.0rc1) and Devise in my Rails application. I
I am beginner in ruby + rails. I have problem to get values in
I am new to Rails routing and I currently have a problem and hope
I have a little problem with my application rails and locale params. I successfully

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.