# STI has\_many :through problem

**URL:** <https://www.ruby-forum.com/t/sti-has-many-through-problem/136003>\
**Category:** Rails\
**Created:** [April 27, 2008, 9:59pm UTC](https://www.ruby-forum.com/t/sti-has-many-through-problem/136003 "2008-04-27T21:59:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![birk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f0a364/32.png) [@birk](https://www.ruby-forum.com/u/birk)\
**Post date:** [April 27, 2008, 9:59pm UTC](https://www.ruby-forum.com/t/sti-has-many-through-problem/136003/1 "2008-04-27T21:59:21Z")

</div>

Hi there,

this is my first post here and I’m pretty new to Rails, too. So be  
kind, please. 😉

I’m building a role-based system and I want some of the roles to have  
their own model/class. So I decided for a STI scenario where I have  
User model, then Salesman and BillingClerk models which are derived  
from the first one. Then, when I instantiate the current user after  
the authentification I create either an instance of the Salesman or  
the BillingClerk class. I’d like to have it like this because I want  
to define associations for each of these models separately - because  
although salesmen and billing clerks relate to the same entity (eg.  
an ad), they relate to it in a different way (salesman has many ads  
through ad assignments and billing clerk has many ads through all of  
his salesmen). I tried two approaches, but none of them worked as I  
wanted:

Here is my first try:

class User \< ActiveRecord::Base  
…  
end

class Salesman \< User  
has\_many :ad\_assignments  
has\_many :ads, :through =\> :ad\_assignments  
belongs\_to :billing\_clerk  
…  
end

class BillingClerk \< User  
has\_many :salesmen  
has\_many :ads, :through =\> :salesmen, :source =\> :billing\_clerk  
…  
end

This approach didn’t work at all:

Mysql::Error: Not unique table/alias: ‘users’: SELECT users.\* FROM  
users INNER JOIN users ON users.id = users.billing\_clerk\_id WHERE  
(users.deleted\_at IS NULL OR users.deleted\_at \> ‘2008-04-27 21:11:05’)  
AND ((users.billing\_clerk\_id = 4) AND ((users.type = ‘Salesman’))) AND  
( (users.`type` = ‘BillingClerk’ ) )

With my second try (although not that clean as the first one) I had  
more luck. After I found out that rails doesn’t know how to handle  
multiple :through associations on the same table (or am I wrong at  
this?), I decided to tell it how to handle ads association in the  
BillingClerk model, so it now looks like this:

class BillingClerk \< User  
has\_many :salesmen  
has\_many :ads, :class\_name =\> ‘Ad’,  
:finder\_sql =\> ‘SELECT ads.\* FROM ads  
LEFT JOIN ad\_assignments ON  
ads.ad\_assignment\_id = ad\_assignments.id  
LEFT JOIN users salesmen ON  
ad\_assignments.user\_id = salesmen.id  
WHERE salesmen.billing\_clerk\_id = #{id}’

…  
end

This approach works. Kind of. I can get an array of Ad objects when i  
call BillingClerk.find(:first).ads. That’s fine. But when i try to get  
some subset of ads through find method, eg.  
BillingClerk.find(:first).ads.find :first, I get just nil. How is that  
possible? I just don’t understand it. Can someone help?

I’m using Rails 1.8.6.

Thank you in advance,  
Milan

---

<div class="post-metadata">

**Author:** ![birk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f0a364/32.png) [@birk](https://www.ruby-forum.com/u/birk)\
**Post date:** [April 28, 2008, 9:25pm UTC](https://www.ruby-forum.com/t/sti-has-many-through-problem/136003/2 "2008-04-28T21:25:41Z")

</div>

Milan,

See if this thread helps at all:

[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/9c63361bbd8ea541/3887b4c8ddbf25e2?hl=en&lnk=st&q=Single+table#3887b4c8ddbf25e2](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/9c63361bbd8ea541/3887b4c8ddbf25e2?hl=en&lnk=st&q=Single+table#3887b4c8ddbf25e2)

The example that AWDWR gives for STI is pretty nice. I’d recommend  
checking it out. (Sorry - not much time today.)

-Kyle
