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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:17:48+00:00 2026-05-26T06:17:48+00:00

I have a tree-like model where in all situations but one , I want

  • 0

I have a tree-like model where in all situations but one, I want to scope the results to only return the roots.

class Licence < ActiveRecord::Base
  default_scope :conditions => { :parent_licence_id, nil }

  belongs_to :parent_licence, :class_name => 'Licence'
  has_many :nested_licences, :class_name => 'Licence',
           :foreign_key => 'parent_licence_id', :dependent => :destroy
end

class User < ActiveRecord::Base
  has_many :licences
end

Using default_scope seemed like an awesome idea because the various models which have associations to Licence (there are about 4 of them) and also any code using find(), would not need to do anything special. The reason it isn’t an awesome idea is that the default scope also applies to the has_many, which results in never finding the children. But the has_many is the only place which needs to bust out of the scope, so as far as “default” behaviour goes, I think this default_scope is quite reasonable.

So is there some good way to get around this specific problem?

Here is one which I am not too fond of because it uses SQL for a nearly trivial query:

has_many :nested_licences, :class_name => 'Licence', :dependent => :destroy,
  :finder_sql => 'SELECT l.* FROM licences l WHERE l.parent_licence_id = #{id}',
  :counter_sql => 'SELECT COUNT(l.*) FROM licences l WHERE l.parent_licence_id = #{id}'

Alternatively is there some way to apply a named scope to an association from the model? e.g. something along the lines of this nonsense-code:

class Licence < ActiveRecord::Base
  named_scope :roots, :conditions => { :parent_licence_id, nil }

  belongs_to :parent_licence, :class_name => 'Licence'
  has_many :nested_licences, :class_name => 'Licence',
           :foreign_key => 'parent_licence_id', :dependent => :destroy
end

class User < ActiveRecord::Base
  has_many :licences, :scope => :roots   # a :scope option doesn't really exist
end

I know I can also do this:

class Licence < ActiveRecord::Base
  named_scope :roots, :conditions => { :parent_licence_id, nil }

  belongs_to :parent_licence, :class_name => 'Licence'
  has_many :nested_licences, :class_name => 'Licence',
           :foreign_key => 'parent_licence_id', :dependent => :destroy
end

class User < ActiveRecord::Base
  has_many :licences, :conditions => { :parent_licence_id, nil }
end

But that really isn’t very DRY. Actually having to do every single query through Licence.roots.find() instead of Licence.find() isn’t very DRY either, to be honest. It’s just asking for a bug to occur where the scope isn’t used.

  • 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-26T06:17:48+00:00Added an answer on May 26, 2026 at 6:17 am

    Try using Licence.unscoped.find()

    btw –
    The documentation for ActiveRecord::Base.unscoped says that chaining unscoped with a named scope method has no effect.
    It is recommended to use the block form of unscoped because chaining unscoped with a named scope does not work. If “sent” (below) is a named_scope the following two statements are the same.

    Message.unscoped.sent
    Message.sent  
    

    fyi rails 2 also has with_exclusive_scope which can be helpful.

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

Sidebar

Related Questions

I have a category tree and i'd like to get all products that are
I have a tree-like model I'd like to show in an NSOutlineView using an
I have Adjacency list mode structure like that and i want to count all
I have tree like following example, where every leaf is object. [1] |--[2] |
I have a tree-like object, managed by Hibernate. The object has a list of
I have a tree walker like this: function: ^(FUNCTION_TOK fcname=IDENTIFIER param=functionParameters*){ a_param_arrayList.add(param); } ;
We have a table with tree structure like this: Id Desc ParentID === A
I have a tree panel and I would like to associate an action to
I have a tree with n elements that store something like |id|parent_id| . I
I am trying to have a tree display custom data that looks like this.

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.