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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:57:06+00:00 2026-05-11T02:57:06+00:00

Right now I have a table called Campaigns that has many Hits, if I

  • 0

Right now I have a table called Campaigns that has many Hits, if I call say:

Campaign.find(30).hits

Which takes 4 seconds, or 4213 ms.

If I call this instead:

 campaign = Campaign.find(30) campaign.hits.count 

Does it still load all of the hits, then count? Or does it see I am counting and avoids loading all of the hits? (Which is currently 300,000+ rows).

I am trying to figure out a smart way to load/count my hits. I am thinking about adding a method to my Campaign.rb model, like:

 def self.total_hits    find :first, :select => 'COUNT(id) as hits', :conditions => ['campaign_id = ?', self.id] end 

I know that query won’t load from the hits table, but that is just an example of counting it from a self made query, apposed to Ruby on Rails doing this for me.

Would this memcache query be more effecient? (I have it running, but doesn’t seem to be any better/faster/slower, just the same speed.)

 def self.hits     Rails.cache.fetch('Campaign_Hits_#{self.campaign_id}', :expires_in => 40) {       find(:first, :select => 'COUNT(id) as hits', :conditions => ['campaign_id = ?', self.campaign_id]).hits     } end 

Any suggestions would be great!

  • 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. 2026-05-11T02:57:06+00:00Added an answer on May 11, 2026 at 2:57 am

    How about:

    Campaign.find(30).hits.count 

    You might also consider adding the following in hit.rb (assuming a one-to-many relationship between campaigns and hits).

    belongs_to :campaign, :counter_cache => true 

    You then need a column in the campaigns table called hits_count. This will avoid hitting hits altogether if you’re only getting the count.

    You can check the API for the full rundown.

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

Sidebar

Related Questions

I am writing a MVC3 project. Right now I have a table which has
I am writing a MVC3 project. Right now I have a table which has
Right now on my (beta) site i have a table called user data which
Let's say that I have a class/table called Images that, as it stands right
I have some code that shows results in a table Right now it will
Let's say I've got a table called [Items] which has a primary key ItemID
I have a class called Stream that has many records in the database, and
Hi I have a rails app which has a table called listings. This table
I'm learning php, and right now I'm stuck. I have code that has to
Right now I have 3 tables: User, Roles, and User_Roles for the many-to-many association.

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.