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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:11:24+00:00 2026-06-08T18:11:24+00:00

In my view, I’ve got a fiddly loop which creates 500 SQL queries (to

  • 0

In my view, I’ve got a fiddly loop which creates 500 SQL queries (to get the info for 500 books). How can I avoid lots of SQL queries by loading a variable up in the controller?

My current (pseudo) code:

controller index action:

@books = Book.scoped.where(:client_id => @client.id).text_search(params[:query])
@feature_root = Book.multiple_summary_details_by_category( @books ) 
  #...returns a hash of books
@features = @feature_root.to_a.paginate(:page => params[:page], :per_page => 4)

index.html.haml

    = render :partial => "feature", :locals => { :features => @features }

_features.html.haml

  - features.each_with_index do |(cat_name, array_of_books), i|  
    %h2
      = cat_name
    - array_of_books[0..10].each do |feature|
      = link_to image_tag(feature[:cover], :class => "product_image_tiny"), book_path(feature[:book])
    # more code
    - array_of_books.sort_by{ |k, v| k["Author"] }.each do |feature|
      - feature.each do |heading,value|
        %span.summary_title
          = heading + ':'
        %span.summary_value
          = value
  • 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-08T18:11:25+00:00Added an answer on June 8, 2026 at 6:11 pm

    What have you tried so far? It should be quite easy with standard ActiveRecord queries as documented in http://guides.rubyonrails.org/active_record_querying.html.

    Also, instead of

    array_of_books.sort_by{ |k, v| k["Author"] }
    

    try something like

    Book.order("author DESC")
    

    (not sure about your exact model here) to let the db do the sorting rather than putting them in an array and let ruby handle it.

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

Sidebar

Related Questions

Django view points to a function, which can be a problem if you want
My view page has a Model which is represented by a List of Students.
View Users page lists all users in the system. You can multiselect users via
My view calls some backend classes which need some user input. When user input
View Here: http://174.132.101.73/~ree/header/ I have a drop down box. I am trying to get
{{view Ember.Select contentBinding=ResAdmin.adminController.priceCategories valueBinding=selectedRestaurant.PriceCategoryID optionLabelPath=content.name optionValuePath=content.id}} how can i add the default value like
my view starts with a focus on a text editor, which causes the keyboard
what view is this in android, I have seen this in iphone can any
The View() method can load Partial Views. Is the difference between View() and PartialView()
view on http://www.eveo.org download site for easy modification: http://www.eveo.org/backup/eveo.rar http://www.eveo.org/backup/eveo.zip As you can see

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.