# STI joins

**URL:** <https://www.ruby-forum.com/t/sti-joins/209782>\
**Category:** Rails\
**Created:** [July 22, 2011, 3:13pm UTC](https://www.ruby-forum.com/t/sti-joins/209782 "2011-07-22T15:13:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [July 22, 2011, 3:13pm UTC](https://www.ruby-forum.com/t/sti-joins/209782/1 "2011-07-22T15:13:39Z")

</div>

I have 2 models set up using single table inheritance (STI) with  
belongs\_to relationships to a third model.

class CoveredEntity \< Entity  
belongs\_to :breach  
…  
end

class BusinessAssociate \< Entity  
belongs\_to :breach  
…  
end

class Breach \< ActiveRecord::Base  
has\_one :covered\_entity  
has\_one :business\_associate  
end

In my index view for breaches controller I am displaying the  
covered\_entity.name and the business\_associate.name (if it exists). I  
want to allow the users to sort the index table by either of these  
values; however, since they use STI and are the same table, just doing  
@breaches.includes(:covered\_entity).includes(:business\_associate).order("#{sort\_column}  
#{sort\_direction}") does not work.

I can get around this by using .joins("inner join (select \* from  
entities where entities.type = ‘CoveredEntity’) as covered\_entities left  
join (select \* from entities where entities.type = ‘BusinessAssociate’)  
as business\_associates) and then I am able to use  
.order(“business\_associate.name”) or .order(“covered\_entities.name”) and  
get the desired results.

I was curious if there is another way to do this that would not require  
me to write out the joins by hand ( passing an alias to association or  
includes for example).

---

<div class="post-metadata">

**Author:** ![Juan\_Alvarado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@Juan\_Alvarado](https://www.ruby-forum.com/u/Juan_Alvarado)\
**Post date:** [July 22, 2011, 9:48pm UTC](https://www.ruby-forum.com/t/sti-joins/209782/2 "2011-07-22T21:48:41Z")

</div>

On Friday, July 22, 2011 7:12:07 AM UTC-6, Juan Alvarado wrote:

> belongs\_to :breach  
> covered\_entity.name and the business\_associate.name (if it exists). I  
> desired results.
> 
> I was curious if there is another way to do this that would not require me  
> to write out the joins by hand ( passing an alias to association or includes  
> for example).

Any reason why you can’t use something like this?

sort\_by\_name\_of = use\_business\_entity ? :business\_associate :  
:covered\_entity  
@breaches = @breaches.joins(sort\_by\_name\_of).order(:name) if  
sort\_by\_name

# works in my quick, test rails 3.0.7 app

Is using #joins abhorrent to you?

Also, as an aside, any reason you don’t/can’t put your “belongs\_to  
:breach”  
relationship in your Entity class (since both STI sub-types both have  
the  
relationship of the same name)?
