Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
get counter nested model mongoid
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Maria Serrano Cáceres  
View profile  
 More options Nov 5 2012, 1:32 pm
From: Maria Serrano Cáceres <maserranocace...@gmail.com>
Date: Mon, 5 Nov 2012 10:32:09 -0800 (PST)
Local: Mon, Nov 5 2012 1:32 pm
Subject: get counter nested model mongoid

2 models:

Class User
include Mongoid::Document
has_many :reports
end

Class Report
include Mongoid::Document
belongs_to :user
end

I need a query to get all users have 6 or more reports, something like:.

Users.where(reports.count > 5)

How can I do it?

Thank you very much!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Durran Jordan  
View profile  
 More options Nov 8 2012, 2:59 am
From: Durran Jordan <dur...@gmail.com>
Date: Thu, 8 Nov 2012 08:59:00 +0100
Subject: Re: [mongoid] get counter nested model mongoid

This query is not possible since MongoDB does not have joins. You would
need to store a "counter cache" for reports on the User in order to do so.
A quick and dirty example:

class User
  include Mongoid::Document
  field :reports_count, type: Integer
  has_many :reports
end

class Report
  include Mongoid::Document
  belongs_to :user

  after_create do |doc|
    doc.user.inc(:reports_count, 1)
  end

  after_destroy do |doc|
    doc.user.inc(:reports_count, -1)
  end
end

2012/11/5 Maria Serrano Cáceres <maserranocace...@gmail.com>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Maria Serrano Cáceres  
View profile  
 More options Nov 11 2012, 11:19 am
From: Maria Serrano Cáceres <maserranocace...@gmail.com>
Date: Sun, 11 Nov 2012 08:19:40 -0800 (PST)
Local: Sun, Nov 11 2012 11:19 am
Subject: Re: [mongoid] get counter nested model mongoid

Thank you Durrand. Is valid for this example instead use this code use this
gem https://github.com/jah2488/mongoid-magic-counter-cache

Thank you very much again Durrand!.

El jueves, 8 de noviembre de 2012 08:59:02 UTC+1, Durran Jordan escribió:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »