Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Counting result "polymorphically"
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
  1 message - 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:
Follow-up To:
Add Cc | Add Follow-up 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 that you hear
 
Geoffroy Gomet  
View profile   Translate to Translated (View Original)
 More options 5 Nov, 13:32
From: Geoffroy Gomet <geoffroy.go...@gmail.com>
Date: Thu, 5 Nov 2009 05:32:58 -0800 (PST)
Local: Thurs 5 Nov 2009 13:32
Subject: Counting result "polymorphically"
Hello,

I have the following situation in a rails application, where A, B and
C are models (objects) and are linked in the following way:

A :has_many B
A :has_many C, :as => :sumtinable

B :belongs_to A
B :has_many C, :as => :sumtinable

C :belongs_to :sumtinable, :polymorphic => true

I want to count all the Cs that belongs to A (directly and through B).
I came up with a query:
select count(*) from C where sumtinable_type='B' AND sumtinable_id IN
(select id from B where A_id=1) OR sumtinable_type='A' AND
sumtinable_id=1
The query works and fill my needs but I would like to know if there's
a Rails (efficient) way of doing the same thing?
I would like the database to do the work and avoid iteration in Ruby.

Regards

Geoffroy


    Reply    Reply to author    Forward  
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 »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google