HighDots Forums  

Counting result "polymorphically"

Ruby On Rails Talk Ruby On Rails programming language mailing list


Discuss Counting result "polymorphically" in the Ruby On Rails Talk forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Geoffroy Gomet
 
Posts: n/a

Default Counting result "polymorphically" - 11-05-2009 , 08:32 AM






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, olymorphic => 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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.