Forum: Ruby on Rails has_many through polymorphic true

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2bd7446a46f3e5eede1527cc9599e5af?d=identicon&s=25 David Beckwith (dbit)
on 2009-03-11 17:12
(Received via mailing list)
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.
2bd7446a46f3e5eede1527cc9599e5af?d=identicon&s=25 David Beckwith (dbit)
on 2009-03-11 17:16
(Received via mailing list)
And it would be cool to be able to say:

@user.student # if the role was present or give nil otherwise.
B211f888a7c3c759166a64c29f9718d3?d=identicon&s=25 Altair (Guest)
on 2009-03-14 13:44
(Received via mailing list)
I believe that the @student.user case is possible, so long as you
change the :through => :rolable, to :through => :role
This topic is locked and can not be replied to.