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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:10:21+00:00 2026-06-11T22:10:21+00:00

I have 2 models class Category < ActiveRecord::Base belongs_to :parent, :class_name => Category has_many

  • 0

I have 2 models

class Category < ActiveRecord::Base
  belongs_to :parent, :class_name => "Category"
  has_many :children,  :class_name => "Category", :foreign_key => "parent_id"
  has_many :products
  attr_accessible :description, :title, :parent

end

class Product < ActiveRecord::Base
  belongs_to :category
end

In particular, Category has a parent item titled “tea” and this item has many children items: “black tea”, “white tea”…

I need to select products that belong to a parent category “tea”. Here is how I’m doing that:

Product.joins(:category=>:parent).where(:category=>{:parent=>{:id=>1}}).all

It throws an exception (unable to format it well)

Product Load (0.8ms)  SELECT `products`.* FROM `products` INNER JOIN `categories` ON `categories`.`id` = `products`.`category_id` INNER JOIN `categories` `parents_categories` ON `parents_categories`.`id` = `categories`.`parent_id` WHERE `parent`.`id` = 1

ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'parent.id' in 'where clause': SELECT `products`.* FROM `products` INNER JOIN `categories` ON `categories`.`id` = `products`.`category_id` INNER JOIN `categories` `parents_categories` ON `parents_categories`.`id` = `categories`.`parent_id` WHERE `parent`.`id` = 1

because of unknown parent.id column.

Obviously, the query should be (it’s working perfect):

    SELECT `products`.* FROM `products` 
    INNER JOIN `categories` ON `categories`.`id` = `products`.`category_id` 
INNER JOIN `categories` as `parents_categories` ON `parents_categories`.`id` = `categories`.`parent_id` WHERE `parents_categories`.`id` = 1

I even tried

Product.joins(:category=>:parent).where(:category.parent=>{:id=>1}).all

and it didn’t help

Please, your thoughts.

  • 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-11T22:10:22+00:00Added an answer on June 11, 2026 at 10:10 pm

    While the operation of joins() here is pretty smart, the where() part isn’t so clever. AFAIK it doesn’t know anything about the joins and really just converts its arguments to strings. As such, try this:

    Product.joins(:category=>:parent).where(:parents_categories=>{:id=>1})
    

    In order to avoid bleeding the name used internally by AR to your code, consider using AREL for your query. There have been some excellent railscasts on that subject recently.

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

Sidebar

Related Questions

I have 2 models: class Video < ActiveRecord::Base belongs_to :categories, :foreign_key => category, :class_name
I have the following models: Post.rb class Post < ActiveRecord::Base belongs_to :category has_many :attachments,
I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <
I have 2 models: class UserPrice < ActiveRecord::Base attr_accessible :price, :product_name belongs_to :user belongs_to
I have 2 models: class Mission < ActiveRecord::Base belongs_to :category end class Category <
I have following models: class Category < ActiveRecord::Base has_many :items default_scope where(:enabled => true,
I currently have 2 models setup: class Topic < ActiveRecord::Base belongs_to :category end class
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have a model with a relationship like this: class Category < ActiveRecord::Base has_many
I have 2 models which have a has_and_belongs_to_many relation: class Category < ActiveRecord::Base has_and_belongs_to_many

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.