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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:56:53+00:00 2026-05-26T16:56:53+00:00

I’ve been having this problem that I just can’t wrap my mind around. I

  • 0

I’ve been having this problem that I just can’t wrap my mind around. I need to get a sum the attribute “views” in my object, however, when using :include, I get a very innacurate sum.

>> c = Category.find 181
>> c.pages.sum(:views, :include => [:tags, :author], :conditions => q)
=> 48448

If I remove the :include, I get the correct sum.

>> c.pages.sum(:views, :conditions => q)
=> 11991

The amount of pages is the same in both queries, so I have no idea why that value could be any different. Going through each page will work as well.

>> c.pages.find(:all, :include => [:tags, :author], :conditions => q).each {|p| a += p.views}; a
=> 11991

:tags and :author do not have a :views attribute or even anything remotely similar, and even specifying the attribute with pages.views yields the same result.

Unfortunately, I need to get that value through the database in order to quickly generate a graph for the views. The q variable used in the conditions is the filter, and the reason the :include is needed. I don’t understand what could be causing this or where that 48448 value is coming from;

I’d appreciate any help in this, and thanks in advance.

  • 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-26T16:56:54+00:00Added an answer on May 26, 2026 at 4:56 pm

    Assume situation

    pages
    id|name    |views
    1 |my_page |10
    
    tags
    id|name   |page_id
    1 |my_tag1|1
    2 |my_tag2|1
    

    using

    :include => [:tags]
    

    you are duplicating page with id=1 two times:

    result
    pages.id|pages.name|pages.views|tags.id|tags.name|tags.page_id
    1       |my_page   |10         |1      |my_tag1  |1
    1       |my_page   |10         |2      |my_tag2  |1
    

    Sum on column pages.views for this result set is 20 not 10.

    [Edited]

    Rewritten query witout using :include

    c.pages.sum(:views, :conditions => ["id in (select distinct tags.page_id from tags where tags.id in (?))", tag_ids]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.