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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:24:50+00:00 2026-05-24T18:24:50+00:00

I have couple of complex SQL and need to convert it into an ActiveRecord

  • 0

I have couple of complex SQL and need to convert it into an ActiveRecord query. Please help me:

My models:

class Product < ActiveRecord::Base
  belongs_to :watch, :counter_cache => true
end

class Watch < ActiveRecord::Base
  belongs_to :category
  has_many :products
end

class Category < ActiveRecord::Base

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

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

end

So Category have ancestry with two level deep, root is make and children is serie.
My SQLs are following:

scope :by_make, lambda { |make_name| Product.find_by_sql("
    SELECT p.* FROM products p INNER JOIN watches w ON p.watch_id = w.id
      INNER JOIN categories series ON w.category_id = series.id
      INNER JOIN categories makes ON series.ancestry = makes.id
      WHERE makes.name LIKE '%#{make_name}%'
    ") unless make_name.blank? }

 scope :by_series, lambda { |series_name| Product.find_by_sql("
      SELECT p.* FROM products p INNER JOIN watches w ON p.watch_id = w.id
        INNER JOIN categories series ON w.category_id = series.id
        WHERE series.name LIKE '%#{series_name}%'
      ") unless series_name.blank? }

Please help to convert those into ActiveRecord queries, because it’s very important not to get array on the end 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-24T18:24:51+00:00Added an answer on May 24, 2026 at 6:24 pm

    The most simple solution is just add where filter at the start of find_by_sql, something like that:

      scope :by_make, lambda { |make_name| where(:watch_id => Watch.find_by_sql("
        SELECT w.* FROM watches w
          INNER JOIN categories series ON w.category_id = series.id
          INNER JOIN categories makes ON series.ancestry = makes.id
          WHERE makes.name LIKE '%#{make_name}%'
        ")) unless make_name.blank? }
    
      scope :by_series, lambda { |series_name| where(:watch_id => Watch.find_by_sql("
          SELECT w.* FROM watches w
            INNER JOIN categories series ON w.category_id = series.id
            WHERE series.name LIKE '%#{series_name}%'
          ")) unless series_name.blank? } 
    

    Should return AR collection.

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

Sidebar

Related Questions

I have a complex .NET Remoting server app that provides a couple of services.
I have a complex build process to generate a couple of python extension modules
So I have a SQL Query as Follows SELECT P.Date, P.CategoryName, P.ProductName, SUM(Quantity) Quantity,
I have a couple of variables that need to be assigned inside a for
So I have a fairly complex applet structure from a couple years back, and
I have a couple of PDF template files with complex content and several blank
I have a rather complex custom control - the custom control has a couple
I have couple questions regarding some C++ rules. Why am I able to call
I have couple resource DLLs that I currently load when application starts using following
I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code

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.