# STI Problem When Using Same Controller

**URL:** <https://www.ruby-forum.com/t/sti-problem-when-using-same-controller/164952>\
**Category:** Rails\
**Created:** [April 7, 2009, 7:28pm UTC](https://www.ruby-forum.com/t/sti-problem-when-using-same-controller/164952 "2009-04-07T19:28:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chenillen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@chenillen](https://www.ruby-forum.com/u/chenillen)\
**Post date:** [April 7, 2009, 7:28pm UTC](https://www.ruby-forum.com/t/sti-problem-when-using-same-controller/164952/1 "2009-04-07T19:28:42Z")

</div>

Hi there,

I created few models with STI, parent model is Account, child models  
are CreditCard, DebitCard, etc… several sub-models, using STI.

Every model works fine individually, but I have another model user  
associated with account

user has\_many accounts  
account belongs\_to user

The problems are :

1. Cant use association to create sub-models, like  
User.first.creditcards.new, but I can use User.first.accounts.new.

2. How can use same controller AccountsController to create different  
sub-models, actually I hardly use Account Model.

codes are as following

[http://pastie.org/439744](http://pastie.org/439744)

---

<div class="post-metadata">

**Author:** ![chenillen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@chenillen](https://www.ruby-forum.com/u/chenillen)\
**Post date:** [April 7, 2009, 7:51pm UTC](https://www.ruby-forum.com/t/sti-problem-when-using-same-controller/164952/2 "2009-04-07T19:51:35Z")

</div>

The user does not know what creditcards are in your user model. You  
have to have an association in the user model if you want to do  
User.first.creditcards.new

has\_many :creditcards

or get the users account and then check the creditcard… Since a user  
has\_many accounts you would have to get an account before you could  
create a creditcard.

So something like User.first.accounts.first.creditcard.new would  
explain better…

Or you could use has\_many through and use the accounts model as the  
through…

has\_many :accounts  
has\_many :creditcards, :through =\> :accounts

---

<div class="post-metadata">

**Author:** ![chenillen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@chenillen](https://www.ruby-forum.com/u/chenillen)\
**Post date:** [April 8, 2009, 5:45am UTC](https://www.ruby-forum.com/t/sti-problem-when-using-same-controller/164952/3 "2009-04-08T05:45:24Z")

</div>

Thanks Freddy,

So rails doesn’t support association inheritance, I know what you  
mean, but I don’t like to put the sub-model association into user  
model,

because i have about 10 sub-models like credit\_card in Account,

I found a plugin called “has\_many\_polymorphs”, this plugin allow you  
access all the association between two parent model.

One other question, if I want to use one controller to create all the  
sub models, how could i write all the controller actions?

Thanks again,

Allen
