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

  • Home
  • SEARCH
  • 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 7088939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:51:43+00:00 2026-05-28T07:51:43+00:00

i have following model setup class Category < ActiveRecord::Base has_ancestry :cache_depth => true, :depth_cache_column

  • 0

i have following model setup

class Category < ActiveRecord::Base

  has_ancestry :cache_depth => true, :depth_cache_column => :depth

  has_many :watches, :dependent => :destroy
  has_many :products, :through => :watches
end

class Watch < ActiveRecord::Base

  belongs_to :category
  has_many :products
end

class Product < ActiveRecord::Base

  belongs_to :watch, :counter_cache => true
  belongs_to :category
end

I need to find products through categories name. Category have 2 levels deep(tree structure). 1 – level is a make, 2 – serie. For now im build this type of search query with the help of meta_search gem

@products = (Product.search :watch_category_name_contains => params[:search]).all.paginate(:page => params[:page])

This works and return all products with serie_name. But watch table always contain only category_id of 2 level category(serie), and im need to be able to search products through makes(1 level category). How can i build this type of query? Thanks!

  • 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-28T07:51:44+00:00Added an answer on May 28, 2026 at 7:51 am

    Well, i see some upvotes coming on my old question, so i will answer. Im finish with raw sql for makes and series queries. Here it is:

      def self.makes_with_products
        find_by_sql "
    SELECT makes.* FROM categories makes
    WHERE ancestry IS NULL AND makes.id IN (
        SELECT series.ancestry FROM products p
        INNER JOIN watches w ON w.id = p.watch_id
        INNER JOIN categories series ON series.id = w.category_id
        WHERE series.ancestry = makes.id AND p.active
      )
    "
      end
    
      def series_with_products
       find_by_sql "
    SELECT series.* FROM categories series
    WHERE series.ancestry = '#{id}'
      AND (
        SELECT COUNT(*) FROM products p
        INNER JOIN watches w ON w.id = p.watch_id
        WHERE w.category_id = series.id AND p.active
      ) > 0
    "
      end
    

    Hope this help someone.

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

Sidebar

Related Questions

In my Store model I have the following: class Store < ActiveRecord::Base has_many :categorizations
I have a following model class Order < ActiveRecord::Base has_many :products, :through => :line_items
I have the following models, with their relevant associations: class User < ActiveRecord::Base has_many
i have the following models setup class Player(models.Model): #slug = models.slugField(max_length=200) Player_Name = models.CharField(max_length=100)
I have the following models set up: class Contact < ActiveRecord::Base belongs_to :band belongs_to
I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
I have following setup. from django.db import models from django.contrib.auth.models import User class Event(models.Model):
I have the following domain model setup using EF code first: public class User
I have the following model setup - a User is interested in projects in
I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage

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.