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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:14:29+00:00 2026-05-18T04:14:29+00:00

Okay, I’m thoroughly stumped on this one. I’m trying to build a menu of

  • 0

Okay, I’m thoroughly stumped on this one. I’m trying to build a menu of published web pages organized by category.

Category.rb:

belongs_to :parent, :class_name => "Category", :foreign_key => "parent_id"
has_many   :children, :class_name => "Category", :foreign_key => "parent_id"
has_many :pages, :documents, :galleries

Page.rb

belongs_to :category

The Page model also has :is_published, so I’m trying to filter on that as well. I am reluctant to post my feeble query attempts, but see no other solution than to beg much smarter people:

(self is @current_website)

self.categories.includes(:children, :pages).where('pages.is_published = 1')

This returns mostly what I need, but not Parent Categories without published pages. For instance, it works great if I have:

Parent Category
- Published Page
- Child Category
-- Published Page

Where it fails is when I have no published pages in the parent, like this:

Parent Category
- Child Category
-- Published Page
- Child Category
-- Published Page

Thanks in advance for any help on this. I’m trying to learn as much as I can about queries, but I’m against the wall on this.

UPDATE: Implementing KandadaBoggu’s suggestion has yielded much better results, this was added to Category.rb

  has_many :published_pages, :class_name => "Page",
                             :conditions => {:is_published => true}

However, when using the following:

self.categories.where(:parent_id => nil).includes({:children => :published_pages},
                                                   :published_pages)

I get the results I need, but I also get empty Parent Categories (no published_pages, no child categories with published pages. An example:

- Parent Category
-- Published Page
- Parent Category
-- NOTHING

My temporary fix was to appended the query with:

reject{|category| category.pages.empty? && category.children.empty?}

Thanks again for your help.

  • 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-18T04:14:30+00:00Added an answer on May 18, 2026 at 4:14 am

    Add a new association called published_pages (apart from your current associations)

    class Category
    
      has_many   :children,        :class_name => "Category", 
                   :foreign_key => "parent_id"
      has_many   :published_pages, :class_name => "Page", 
                   :conditions  => { :is_published => true }
    
    end
    

    Now you can get all the categories as follows:

    self.categories.includes(:children, :published_pages)
    

    If you are interested in learning why your approach didnt work, read the Rails documentation (scroll 10-15 lines after the Eager loading of associations section). I have included the relevant snippet below:

    For example

    Post.includes([:author, :comments]).where(['comments.approved = ?', true]).all
    

    This will result in a single SQL query with joins along the lines of:

    LEFT OUTER JOIN comments ON comments.post_id = posts.id and 
    LEFT OUTER JOIN authors  ON authors.id = posts.author_id. 
    

    Note that using conditions like this can have unintended consequences.
    In the above example posts with notion approved comments are not returned
    at all, because the conditions apply to the SQL statement as a whole
    and not just to the association. You must disambiguate column
    references for this fallback to happen, for example :order =>
    “author.name DESC” will work but :order => “name DESC” will not.

    To eager load filtered rows of an association, use an association with conditions:

    class Post < ActiveRecord::Base
      has_many :approved_comments, :class_name => 'Comment', 
                 :conditions => ['approved = ?', true]
    end
    
    Post.find(:all, :include => :approved_comments)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay I've been trying to research how to do this and have failed. I
Okay I've spent the last hour trying to find a solution to this but
Okay this question is very simple: I have a facebook page, and a website.
Okay, I feel a bit foolish for having to ask this but I guess
Okay, this is probably a very basic question; but, I'm just getting back in
Okay, I've got an interesting one (well, interesting to me, anyway :) ). I've
Okay, so I've got something as simple as this: <audio autoplay='autoplay' loop='loop' id='audio' controls>
Okay, this gets a little convoluted, so bear with me. I'll try and keep
Okay, first off, i started (trying) to learn Java about 2 days ago. im
Okay, I really know this has GOT to be the long way around doing

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.