Folks, I have setup Single Table Inheritance with my User model. Now the user can have multiple roles. In my situation (a school app) the user can be a Teacher (through which I want to access the students, grades etc) and the same user can also be a Parent (through which I want to access the children info). In STI, the type column in the user table can store only one type i.e. either Parent or Teacher. So when this user logs in and I want to access students at one time and children at another - through Teacher or Parent models respectively. The model hierarchy is as below - class User < ActiveRecord::Base end class Parent < User # relationships and functionality to work on children end class Teacher < User # relationships and functionality to work on students and grades etc end Searched around but can''t find anything on this. It doesn''t look like I can accomplish this through STI. However I have the Parent and Teacher functionality setup through these STI models. What is the best way to model/enable this? Thanks for any pointers. -S -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 18, 6:45 pm, skt <stibre...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Folks, > > I have setup Single Table Inheritance with my User model. Now the user > can have multiple roles. In my situation (a school app) the user can > be a Teacher (through which I want to access the students, grades etc) > and the same user can also be a Parent (through which I want to access > the children info). In STI, the type column in the user table can > store only one type i.e. either Parent or Teacher. So when this user > logs in and I want to access students at one time and children at > another - through Teacher or Parent models respectively. > > The model hierarchy is as below - > > class User < ActiveRecord::Base > end > > class Parent < User > # relationships and functionality to work on children > end > > class Teacher < User > # relationships and functionality to work on students and grades etc > end > > Searched around but can''t find anything on this. It doesn''t look like > I can accomplish this through STI. However I have the Parent and > Teacher functionality setup through these STI models. What is the best > way to model/enable this?I did this with a pretty gross hack on an app a while back - the roles were designed to be mutually exclusive, then the client changed their mind 4+ months in. The trick was to disambiguate the records by fiddling with the email - in my case, I added the (underscored) role to the email. So bob-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org who was (for instance) a Parent and a Teacher would have two records: Parent: email_address = ''bob+parent-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org'' Teacher: email_address = ''bob+teacher-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org'' I had to tweak the code that handled login, and add some callbacks to keep the records in sync (as well as always de-mangling the emails for display). From the UI side, I added a screen to allow users to switch between each of the roles; for the app, this made sense as each role had a somewhat different set of navigation tabs + view permissions. Not the cleanest solution, but it beat rewriting everything to accommodate multiple roles per user. --Matt Jones -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
skt wrote in post #975891:> Folks, > > I have setup Single Table Inheritance with my User model. Now the user > can have multiple roles.[...] Time to get rid of the STI, then! Do User habtm :roles instead. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Matt Jones wrote in post #975916:> On Jan 18, 6:45pm, skt <stibre...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> # relationships and functionality to work on students and grades etc >> end >> >> Searched around but can''t find anything on this. It doesn''t look like >> I can accomplish this through STI. However I have the Parent and >> Teacher functionality setup through these STI models. What is the best >> way to model/enable this? > > I did this with a pretty gross hack on an app a while back - the roles > were designed to be mutually exclusive, then the client changed their > mind 4+ months in. The trick was to disambiguate the records by > fiddling with the email - in my case, I added the (underscored) role > to the email. So bob-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org who was (for instance) a Parent and a > Teacher would have two records: > > Parent: email_address = ''bob+parent-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org'' > Teacher: email_address = ''bob+teacher-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org''That''s a dreadful idea. You''re essentially keeping the user from meaningfully pluscoding his e-mail address.> > I had to tweak the code that handled login, and add some callbacks to > keep the records in sync (as well as always de-mangling the emails for > display). > > From the UI side, I added a screen to allow users to switch between > each of the roles; for the app, this made sense as each role had a > somewhat different set of navigation tabs + view permissions. > > Not the cleanest solution, but it beat rewriting everything to > accommodate multiple roles per user.No it didn''t. That''s a dreadful hack, and you should rip it out and do it right (which would probably have taken no longer...)> > --Matt JonesBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 18, 6:43 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Matt Jones wrote in post #975916: > > > > > > > > > > > On Jan 18, 6:45pm, skt <stibre...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> # relationships and functionality to work on students and grades etc > >> end > > >> Searched around but can''t find anything on this. It doesn''t look like > >> I can accomplish this through STI. However I have the Parent and > >> Teacher functionality setup through these STI models. What is the best > >> way to model/enable this? > > > I did this with a pretty gross hack on an app a while back - the roles > > were designed to be mutually exclusive, then the client changed their > > mind 4+ months in. The trick was to disambiguate the records by > > fiddling with the email - in my case, I added the (underscored) role > > to the email. So b...-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org who was (for instance) a Parent and a > > Teacher would have two records: > > > Parent: email_address = ''bob+par...-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org'' > > Teacher: email_address = ''bob+teac...-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org'' > > That''s a dreadful idea. You''re essentially keeping the user from > meaningfully pluscoding his e-mail address. > > > > > I had to tweak the code that handled login, and add some callbacks to > > keep the records in sync (as well as always de-mangling the emails for > > display). > > > From the UI side, I added a screen to allow users to switch between > > each of the roles; for the app, this made sense as each role had a > > somewhat different set of navigation tabs + view permissions. > > > Not the cleanest solution, but it beat rewriting everything to > > accommodate multiple roles per user. > > No it didn''t. That''s a dreadful hack, and you should rip it out and do > it right (which would probably have taken no longer...) > > > > > --Matt Jones > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > -- > Posted viahttp://www.ruby-forum.com/.Thanks for the feedback folks. I think I resolved that with a simple tweak by making Teacher inherit from Parent as this is an "is-a" relationship. class Teacher < Parent # Gives access to Parent functionality end The type column in User table says "Teacher" and the User (Teacher) object has access to Parent''s functionality. Any potential pitfalls that I may not be seeing with this? Thanks, -S -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.