# Find :all in subclass within STI finds parents instead

**URL:** <https://www.ruby-forum.com/t/find-all-in-subclass-within-sti-finds-parents-instead/167670>\
**Category:** Rails\
**Created:** [May 11, 2009, 10:29am UTC](https://www.ruby-forum.com/t/find-all-in-subclass-within-sti-finds-parents-instead/167670 "2009-05-11T10:29:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b2d939/32.png) [@patrick](https://www.ruby-forum.com/u/patrick)\
**Post date:** [May 11, 2009, 10:29am UTC](https://www.ruby-forum.com/t/find-all-in-subclass-within-sti-finds-parents-instead/167670/1 "2009-05-11T10:29:29Z")

</div>

All,

I am relative new to Rails and struggle with following STI issue:

A parent class “Reading” has many subclasses “ElectricityReading”,  
“GasReading”, …

I wrote a method in the Reading class to update calculated columns in  
each record, the logic goes like this:

1. Find first record

first = find(:first, :conditions =\> [“unit\_id = ?”, unit\_id], :order  
=\> “date asc” )

# get values of some columns and store in local variables

1. Find all records, perform the calculations and store in calculated  
fields

find(:all, :conditions =\> [“unit\_id = ?”, unit\_id], :order =\> “date  
asc” ).each do |r|

# get column values of r, calculate and save

1. Controller calls the method after each create or entry  
ElectricityReading.calculate(unit\_id)

The problem:

Instead of

SELECT \* FROM “readings” WHERE (unit\_id = 1) AND ( (“readings”.“type”  
= ‘ElectricityReading’ ) Limit 1  
SELECT \* FROM “readings” WHERE (unit\_id = 1) AND ( (“readings”.“type”  
= ‘ElectricityReading’ )

I get

SELECT \* FROM “readings” WHERE (“readings”.“id” = ‘10’) LIMIT 1  
SELECT \* FROM “readings” WHERE (“readings”.“id” = ‘10’)

So the calculations are performed on all Reading objects rather than  
only the ElectricityReading object.

The funny thing is that it seems to work as expected in the console,  
but fails as above using the built in server.

Is this a bug in STI? Would it be solved by using a different type of  
inheritance?

---

<div class="post-metadata">

**Author:** ![patrick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b2d939/32.png) [@patrick](https://www.ruby-forum.com/u/patrick)\
**Post date:** [May 11, 2009, 2:48pm UTC](https://www.ruby-forum.com/t/find-all-in-subclass-within-sti-finds-parents-instead/167670/2 "2009-05-11T14:48:36Z")

</div>

Check the lines in the log above SELECT \* FROM “readings” WHERE  
(“readings”.“id” = ‘10’) LIMIT 1

Rails tends to break up long SQL statements into smaller ones. More  
information about your code snippet is required to make any guesses.  
Have a look at

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.
