I have the following situation in a rails application, where A, B and
C are models (objects) and are linked in the following way:
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
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.