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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:32:27+00:00 2026-06-16T08:32:27+00:00

I have two different models with a 1:N relation. Let’s name them ‘myobject’ and

  • 0

I have two different models with a 1:N relation.
Let’s name them ‘myobject’ and ‘related’

class Myobject < ActiveRecord::Base
  has_many :related
  scope :without_related, includes(:related).select{ |o| o.related.size == 0 }
end
class Related < ActiveRecord::Base
end

The defined scope seems to work great as long as I don’t create new assignments from Myobjects to Related:

  • Direct rails c command “Myobject.includes(:related).select … (as defined in Scope) works as expected
  • Calls to scope “Myobject.without_related” still return objects that have been assigned in the meantime

It seems that this can be fixed by restarting the rails console or restarting Webrick.
But I can’t always restart a webapplication only because a relation between objects has been changed 😉

Is there any way to fix this problem or to write the scope in a better way?

PS: I need this query as scope to pass its name as group_method to a grouped_select in the form of the Myobject model

  • 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-16T08:32:28+00:00Added an answer on June 16, 2026 at 8:32 am

    I would recommend you to use counter_cache for this, you need to add column *related_count* of type int to Myobject, make migration and then you will be able to do so:

    class Myobject < ActiveRecord::Base
      has_many :related
      scope :without_related, where(related_count: 0)
    end
    
    class Related < ActiveRecord::Base
      belongs_to :myobject, counter_cache: true
    end
    

    After that you will have super fast scope for getting all objects with no related records and an a count of that objects as well

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

Sidebar

Related Questions

I have a transaction (extends ActiveRecord::Base). I have two different types of transactions, a
I currently have two different models: User and Project . The User model has
In my PHP project I have two different models that have a function with
If I have two models that are very different, is it defeating the purpose
I have a duplicate bridge scoring application with two different sorting modes. Let me
I have two different buttons on my page. I want them both to be
I have two different models in my application App.Foo = DS.Model.extend({ title: DS.attr('string') });
I have two tables, both named Language in two different schemas, lets call them
I have two entities: ResourceFile: @Entity @Table(name = resource_file) public class ResourceFile extends IdEntity<Integer>
I have two different models. In both models some of the validations are the

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.