# Has\_many through polymorphic true

**URL:** <https://www.ruby-forum.com/t/has-many-through-polymorphic-true/162589>\
**Category:** Rails\
**Created:** [March 11, 2009, 5:12pm UTC](https://www.ruby-forum.com/t/has-many-through-polymorphic-true/162589 "2009-03-11T17:12:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [March 11, 2009, 5:12pm UTC](https://www.ruby-forum.com/t/has-many-through-polymorphic-true/162589/1 "2009-03-11T17:12:21Z")

</div>

This would be cool to have in Rails.

class User \< ActiveRecord::Base  
has\_many :roles  
end

class Role \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :rolable, :polymorphic =\> true  
end

class Student \< ActiveRecord::Base  
has\_one :role, :as =\> :rolable  
has\_one :user, :through =\> :rolable # \* This is not possible  
yet in Rails (I think).  
end

class Teacher \< ActiveRecord::Base  
has\_one :role, :as =\> :rolable  
has\_one :user, :through =\> :rolable # \* This is not possible  
yet in Rails.  
end

Then be able to say:

@student.user # Instead of having to say @student.role.user

Or even better be able to say @student.email when `email` was a column  
of User but not Student.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [March 11, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/has-many-through-polymorphic-true/162589/2 "2009-03-11T17:16:29Z")

</div>

And it would be cool to be able to say:

@user.student # if the role was present or give nil otherwise.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [March 14, 2009, 1:44pm UTC](https://www.ruby-forum.com/t/has-many-through-polymorphic-true/162589/3 "2009-03-14T13:44:54Z")

</div>

I believe that the @student.user case is possible, so long as you  
change the :through =\> :rolable, to :through =\> :role
