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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:47:48+00:00 2026-06-13T08:47:48+00:00

Setup For this question, I’ll use the following three classes: class SolarSystem < ActiveRecord::Base

  • 0

Setup

For this question, I’ll use the following three classes:

class SolarSystem < ActiveRecord::Base
  has_many :planets

  scope :has_earthlike_planet, joins(:planets).merge(Planet.like_earth)
end

class Planet < ActiveRecord::Base
  belongs_to :solar_system
  belongs_to :planet_type

  scope :like_earth, joins(:planet_type).where(:planet_types => {:life => true, :gravity => 9.8})
end

class PlanetType < ActiveRecord::Base
  has_many :planets

  attr_accessible :gravity, :life
end

Problem

The scope has_earthlike_planet does not work. It gives me the following error:

ActiveRecord::ConfigurationError: Association named ‘planet_type’ was
not found; perhaps you misspelled it?

Question

I have found out that this is because it is equivalent to the following:

joins(:planets, :planet_type)...

and SolarSystem does not have a planet_type association. I’d like to use the like_earth scope on Planet, the has_earthlike_planet on SolarSystem, and would like to avoid duplicating code and conditions. Is there a way to merge these scopes like I’m attempting to do but am missing a piece? If not, what other techniques can I use to accomplish these goals?

  • 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-13T08:47:49+00:00Added an answer on June 13, 2026 at 8:47 am

    Apparently, at this time you can only merge simple constructs that don’t involve joins. Here is a possible workaround if you modify your models to look like this:

    class SolarSystem < ActiveRecord::Base
      has_many :planets
      has_many :planet_types, :through => :planets
    
      scope :has_earthlike_planet, joins(:planet_types).merge(PlanetType.like_earth)
    end
    
    class Planet < ActiveRecord::Base
      belongs_to :solar_system
      belongs_to :planet_type
    
      scope :like_earth, joins(:planet_type).merge(PlanetType.like_earth)
    end
    
    class PlanetType < ActiveRecord::Base
       has_many :planets
    
       attr_accessible :gravity, :life
    
       scope :like_earth, where(:life => true, :gravity => 9.8)
    end
    

    ** UPDATE **

    For the record, a bug was filed about this behavior – hopefully will be fixed soon…

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

Sidebar

Related Questions

[NOTE: My NinjectDependencyResolver class is at the bottom of this question.] Setup: In an
Based the accepted answer to this question I've setup a NetBeans/tomcat environment. In testing
I have this setup in my models: class Author(models.Model): name = models.CharField(max_length=100) class Topic(models.Model):
I have a similar setup shown in this question and was wondering if I
Following up on this question: Kiosk Applications - OS X programming - Multiple monitors
This question asks how to setup your path variable in Windows to include the
I see this question asked all over the internet, and I've tried following them
Similar to this question , but my setup is a little different. I've written
Possible Duplicate: MySQL latitude and Longitude table setup I know this question has probably
Fair warning: The setup for this question is long, so be patient and stay

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.