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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
And it would be cool to be able to say: @user.student # if the role was present or give nil otherwise. On Mar 12, 12:11 am, David Beckwith <dbitsoluti...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I believe that the @student.user case is possible, so long as you change the :through => :rolable, to :through => :role On Mar 11, 10:15 am, David Beckwith <dbitsoluti...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> And it would be cool to be able to say: > > @user.student # if the role was present or give nil otherwise. > > On Mar 12, 12:11 am, David Beckwith <dbitsoluti...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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. > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---