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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:05:33+00:00 2026-05-15T07:05:33+00:00

For example: class Product has_many :sales_orders def total_items_deliverable self.sales_orders.each { |so| #sum the total

  • 0

For example:

class Product
  has_many :sales_orders

  def total_items_deliverable
    self.sales_orders.each { |so| #sum the total }
    #give back the value
  end
end

class SalesOrder
  def self.deliverable
    # return array of sales_orders that are deliverable to customer
  end
end
  1. SalesOrder.deliverable #give all sales_orders that are deliverable to customer
  2. pa = Product.find(1)
  3. pa.sales_orders.deliverable #give all sales_orders whose product_id is 1 and deliverable to customer
  4. pa.total_so_deliverable

The very point that i’m going to ask is: how many times SalesOrder.deliverable is actually computed, from point 1, 3, and 4, They are computed 3 times that means 3 times access to database

so having total_so_deliverable is promoting a fat model, but more database access. Alternatively (in view) i could iterate while displaying the content, so i ends up only accessing the database 2 times instead of 3 times.

Any win win solution / best practice to this kind of problem ?

  • 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-15T07:05:33+00:00Added an answer on May 15, 2026 at 7:05 am

    Look in your environment log (e.g. log/development.log) if the query is a cache you’ll see:

    CACHE (0.0ms)   SELECT * FROM `widgets`....
    

    or

    Widget Load (0.4ms)   SELECT * FROM `widgets`....
    

    for a database query.

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

Sidebar

Related Questions

I will go straight to the example: class Foo: @execonce def initialize(self): print 'Called'
I have the following example class: Test.h: @interface Test : UIButton { NSString *value;
I have the following model associations: class Product < ActiveRecord::Base has_many :prices class Price
OK, the title is not that good... Here is the example : class Product
class Category < ActiveRecord::Base has_and_belongs_to_many :products end class Product < ActiveRecord::Base has_and_belongs_to_many :categories end
Given the following models, using rails 2.1: class Product < ActiveRecord::Base has_many :from_sources, :class_name
Example class in pseudocode: class SumCalculator method calculate(int1, int2) returns int What is a
Example: class MyClass { Composition m_Composition; void MyClass() { m_Composition = new Composition( this
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
I have models (simplified example): class Group(models.Model): name = models.CharField(max_length = 32) class Person(models.Model):

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.