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

  • Home
  • SEARCH
  • 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 6629643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:17:22+00:00 2026-05-25T22:17:22+00:00

A controller points to a view. In that view is it acceptable to find

  • 0

A controller points to a view. In that view is it acceptable to find objects <% @XXX = XXXX.where(..... %> or is that bad?

Trying to work through performance issues which is why I ask. 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-25T22:17:23+00:00Added an answer on May 25, 2026 at 10:17 pm

    Putting query logic in the model has more to do with maintainability then it does with performance. Since most of the ActiveRecord/ARel logic deals with lightweight relation objects that only trigger the actual query based on certain methods, generally those provided via Enumerable (each/map/inject/all/first), which are usually called from the view anyway, the actual query gets triggered in the view, and not anywhere else.

    Here’s an example of the difference between limit(3) and first(3) from an app I’m working on atm.

    ruby-1.9.2-p180 :018 > PressRelease.limit(3).is_a? ActiveRecord::Relation
     => true 
    ruby-1.9.2-p180 :019 > PressRelease.first(3).is_a? ActiveRecord::Relation
      PressRelease Load (2.8ms)  SELECT "press_releases".* FROM "press_releases" ORDER BY published_at DESC
     => false
    

    As you can see, limit does not actually trigger a query, where first does.

    When it comes to performance you are usually trying to ensure that your queries are not executed in your controller/model so that you can wrap them in a cache block within your view, thus eliminating that query from most requests. In this case you really want to make sure your not executing the query in your controller by calling any of the Enumerable methods.

    A quick example of a blog that lists the last 10 blog posts on the home page that is setup with caching might look like this.

    # app/controllers/posts_controller.rb
    class PostsController < ApplicationController
      def index
        # Something like this would trigger the query at this point and should be
        # avoided in the controller
        # @posts = Post.first(10)
        # So @posts here will be the Relation returned from the last_ten scope, not
        # an array
        @posts = Post.last_ten
      end
      ...
    end
    
    # app/models/post.rb
    class Post < ActiveRecord::Base
      # Will return an ActiveRecord::Relation
      scope :last_ten, order('created_at DESC').limit(10)
    end
    
    # app/views/posts/index.html.erb
    <ul>
      # The query will actually trigger within the cache block on the call to each,
      # preventing the query from running each time and also reducing the template
      # rendering within the cache block.
      <%= cache(posts_cache_key) do %>
        <% @posts.each do |post| %>
          ..
        <% end %>
      <% end %>
    </ul>
    

    For clarity, all this would be the exact same as doing

    # app/views/posts/index.html.erb
    <ul>
      <%= cache(posts_cache_key) do %>
        <% Post.order('created_at DESC').limit(10).each do |post| %>
          ...
        <% end %>
      <% end %>
    </ul>
    

    Except that if you now want to modify the logic for how it pulls the query, say you wanted to add something like where(:visible => true).where('published_at' <= Time.now) your jumping into your view instead of making changes in the model where the logic should be. Performance wise the difference is insignificant, maintenance-wise the latter turns into a nitemare rather quickly.

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

Sidebar

Related Questions

I have a controller with a method that points to a view. How do
i have a view controller that uses the getDistance method for calculating the distance
I have a View Controller I'm reusing to edit individual objects (NSString, NSNumber, NSDate)
I'm trying to test a method on the application controller that will be used
My Controller Action accepts int id. What to do when calling from View ActionLink
In controller level i used [ValidateInput(false)] and ValidateRequest=false in the view. Everything is fine
I came across a controller in an older set of code (Rails 1.2.3) that
Given a controller method like: def show @model = Model.find(params[:id]) respond_to do |format| format.html
I'm working on a view-based iPhone app that has the following flow: search ->
I have a set of drop down controls on a view that are linked

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.