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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:41:34+00:00 2026-05-22T20:41:34+00:00

I am trying to do a QUERY in my controller to get a list

  • 0

I am trying to do a QUERY in my controller to get a list of suppliers with a category ID.

I have my models set up like this.

  class Supplier < ActiveRecord::Base
    has_and_belongs_to_many :sub_categories
  end

  class Category < ActiveRecord::Base
    has_many :sub_categories
  end

  class SubCategory < ActiveRecord::Base
    belongs_to :category
    has_and_belongs_to_many :suppliers
  end

A supplier can have Many sub_categories that are under one single category. So i can grab the category of a supplier by doing this.

  @supplier.sub_categories.first.category.name

This returns the category that the supplier comes under because they have to have at least 1 sub category which is then linked to a category.

What i am trying to do is by passing a category_id i wish to return all suppliers that come under that category.
I had it written like this but it doesnt seem to be working.

  @category = Category.find(params[:category_id])
  @suppliers = Supplier.where('sub_category.first.category.id = ?', @category.id)

i get the following sql error

  Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.id = 20)' at line 1: SELECT     `suppliers`.* FROM       `suppliers`  WHERE     (sub_category.first.category.id = 20)
  • 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-22T20:41:35+00:00Added an answer on May 22, 2026 at 8:41 pm

    Well, that’s certainly an sql error. The stuff inside the where() call gets translated directly to SQL, and that’s not sq;l. 🙂

    You need to join tables together. I’m assuming there’s a sub_category_suppliers table that completes the habtm association. (BTW, I much prefer to use has_many :through exclusively)

    I think it would be something like this:

    Supplier.joins(:sub_category_suppliers => :sub_categories).
             where('sub_categories.category_id =?', @category.id).
             group('suppliers.id')
    

    As Caley Woods suggested, this should be placed in the Supplier model as a scope:

    scope :by_category, lambda { |category_id|
      joins(:sub_category_suppliers => :sub_categories).
             where('sub_categories.category_id =?', category_id).
             group('suppliers.id')
    }
    

    and then called as Supplier.by_category(@category.id)

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

Sidebar

Related Questions

Trying to query a 'Favorites' model to get a list of items a user
Before trying to query the AD server I would like to check if it
I am trying to query the windows desktop search API using SQL. I have
I am trying to query an .xls spreadsheet with VBScript, but I have run
I'm trying to query a set of buldings listed on a table, these buildings
I am trying to get a control (not a controller -- a subclass of
I have been trying to get a simple example of the jquery-ui autocomplete to
I'm trying to get familiar with CI and I have run into a problem
Okay, so I'm trying to create a Call class on top of my Controller,
Firstly anyone else trying to get onto forum.hibernate.org. I have been trying for a

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.