# Polymorphic STI

**URL:** <https://www.ruby-forum.com/t/polymorphic-sti/131140>\
**Category:** Rails\
**Created:** [March 6, 2008, 12:20pm UTC](https://www.ruby-forum.com/t/polymorphic-sti/131140 "2008-03-06T12:20:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![miv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@miv](https://www.ruby-forum.com/u/miv)\
**Post date:** [March 6, 2008, 12:20pm UTC](https://www.ruby-forum.com/t/polymorphic-sti/131140/1 "2008-03-06T12:20:37Z")

</div>

For fun, I wanted to write a wallet application. There must be  
tranasctions with came in to flavors - income and spendings. After  
playing for a while, i wanted to make everything pretty.

I have something like:

class FinancialTransction \< AR::Base  
belongs\_to :transactinable, :polymorphic =\> true  
end

class Income \< AR::Base  
end

class Spending \< AR::Base  
end

Then i though - why should i have almost empty models just for the  
sake of saving id. I tried this and that, but nothing works. It’s  
either empty transactionable\_type or two separate records (first  
without type, second with).

My question: how to implement this? is there some beautiful way?

---

<div class="post-metadata">

**Author:** ![miv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/258eb7/32.png) [@miv](https://www.ruby-forum.com/u/miv)\
**Post date:** [March 6, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/polymorphic-sti/131140/2 "2008-03-06T20:47:06Z")

</div>

Assuming that you want STI:

create\_table :financial\_transactions do |t|  
t.string :transaction\_type  
…  
end

class FinancialTransaction \< AR:Base  
self.inheritance\_column :transaction\_type  
…  
end

class Income \< FinancialTransaction  
…  
end

class Spending \< FinancialTransaction  
…  
end
