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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:41:36+00:00 2026-05-11T21:41:36+00:00

My Rails app is starting to need complicated queries. Should I just start using

  • 0

My Rails app is starting to need complicated queries. Should I just start using raw SQL queries? What is the trend in the Rails community?

Update:

I do not have written queries right now, I wanted to ask this question before I start. But here is an example of what I want to do:

I have books which have categories. I want to say-

Give me all books that were: 
-created_at (added to store) between date1 and date2
-updated_at before date3
-joined with books that exist in shopping carts right now

I haven’t written the query yet but I think the rails version will be something like this:

books_to_consider = Book.find(:all, 
                       :conditions => "created_at <= '#{date2}' AND created_at >= '#{date1}' AND updated_at <= '#{date3}'",
                       :joins => "as b inner join carts as c on c.book_id = b.id")

I am not saying ActiveRecord can’t handle this query, but is it more accepted to go with raw SQL for readability (or maybe there are other limitations I don’t know of yet)?

  • 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-11T21:41:36+00:00Added an answer on May 11, 2026 at 9:41 pm

    The general idea is to stick to ActiveRecord-generated queries as much as possible, and use SQL fragments only where necessary. SQL fragments are explicitly supported because the creators of ActiveRecord realised that SQL cannot be completely abstracted away.

    Using the the find method without SQL fragments is generally rewarded with better maintainability. Given your example, try:

    Book.find(:all,
      :conditions => ["created_at >= ? AND created_at <= ? AND updated_at <= ?", 
                      date1, date2, date3]
      :include => :carts)
    

    The :inlude => :carts will do the join if you added has_many :carts to your Book model. As you can see, there does not have to be much SQL involved. Even the quoting and escaping of input can be left to Rails, while still using SQL literals to handle the >= and <= operators.

    Going a little bit further, you can make it even clearer:

    class Book < AciveRecord::Base
      # Somewhere in your Book model:
      named_scope :created_between, lambda { |start_date, end_date|
        { :conditions => { :created_at => start_date..end_date } }
      }
      named_scope :updated_before, lambda { |date|
        { :conditions => ["updated_at <= ?", date] }
      }
      # ...
    end
    
    Book.created_between(date1, date2).updated_before(date3).find(:all,
      :include => :carts)
    

    Update: the point of the named_scopes is, of course, to reuse the conditions. It’s up to you to decide whether or not it makes sense to put a set of conditions in a named scope or not.

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

Sidebar

Related Questions

Just starting to learn Ruby on Rails. I'm using RoR 3. I have read
I have a Rails app that I need to deploy. Here are the facts:
I'm starting a Rails app which will use a lot of javascript. What's the
Starting to develop my first Ruby on Rails app with postgresql on Ubuntu. I
Our rails app is designed as a single code base linking to multiple client
My Rails-app has a sign in box with a remember me checkbox. Users who
In my rails app I use the validation helpers in my active record objects
On my rails app I have a list of items (like a task list)
I have a rails app that I have serving up XML on an infrequent
I have a Rails app that I have successfully tested with Mongrel and Webkit.

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.