Forum: Ruby design advice for newbie

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.
Ff5abdc01af5ecb5bf583174cbeac909?d=identicon&s=25 John Goetz (jgoetz)
on 2008-11-10 00:19
Hello,
I have a typical class called Users. Each user has a Role (one of
administrator, teacher, student, parent, or trustee). How do I design my
controller/views/routes to best implement links to administer each
group, like this:
Student Administration
Parent Administration
Teacher Administration
Trustee Administration
Administrator Administration

Right now, each of these links activates the user_controller and lists
all users (for example the student admin link: <%= link_to "Student
Administration", :controller => 'users', :action => 'index' %>).
I would like each link to get only the users having the appropriate
role. I know I can create the link_to with an additional parameter
(e.g., role => 'teacher') which will change the route to
'/users?role=teacher', but is this the correct, RESTful, Rails way of
doing this? If not, can you please point me in the right direction or
towards some guidelines? Thanks!
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2008-11-10 07:49
(Received via mailing list)
On Sun, Nov 9, 2008 at 5:17 PM, John Goetz <goetz.john@gmail.com> wrote:
> Hello,
> I have a typical class called Users. Each user has a Role (one of
> administrator, teacher, student, parent, or trustee). How do I design my
> controller/views/routes to best implement links to administer each
> group, like this:
> Student Administration
> Parent Administration
> Teacher Administration
> Trustee Administration
> Administrator Administration

This question probably belongs in the Rails list/group.

In any case, I don't know your situation, but; are you sure a trustee
administer cannot also be a student administer, or a a parent
administer also be a trustee administer.  Sound like a weird relation
you are trying to force.  If they are mutually exclusive, fine, but
I've never seen a setup like this.

The problem is, I guess, that you may have to make the shoe fit later
if you want to go that route.

Todd
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-11-10 09:48
John Goetz wrote:
> I would like each link to get only the users having the appropriate
> role. I know I can create the link_to with an additional parameter
> (e.g., role => 'teacher') which will change the route to
> '/users?role=teacher', but is this the correct, RESTful, Rails way of
> doing this? If not, can you please point me in the right direction or
> towards some guidelines? Thanks!

This sounds fine to me, but for an alternative approach you could
consider Single Table Inheritance. That is, you have separate classes
for Student, Parent, Teacher etc, but they all live in the users table,
and can all be considered as a User. Teacher.find(...) will
automatically be restricted to User.find(:conditions =>
"type='Teacher'")

http://ar.rubyonrails.com/classes/ActiveRecord/Base.html

Scroll down to "Single table inheritance" Also, the book "Agile Web
Development with Rails" has a section on this (but don't buy the 2nd
edition now, wait for the 3rd edition due in December)

However as already mentioned, this may be a Bad Idea. It's quite
possible for the same person to be both a student and a parent, or a
teacher and a trustee.

Person.has_and_belongs_to_many :roles may be a better way of modelling
this. Admittedly the SQL finders get a bit more awkward.
Ff5abdc01af5ecb5bf583174cbeac909?d=identicon&s=25 John Goetz (jgoetz)
on 2008-11-10 16:05
Todd Benson wrote:
> On Sun, Nov 9, 2008 at 5:17 PM, John Goetz <goetz.john@gmail.com> wrote:
>> Hello,
>> I have a typical class called Users. Each user has a Role (one of
>> administrator, teacher, student, parent, or trustee). How do I design my
>> controller/views/routes to best implement links to administer each
>> group, like this:
>> Student Administration
>> Parent Administration
>> Teacher Administration
>> Trustee Administration
>> Administrator Administration
>
> This question probably belongs in the Rails list/group.
>
> In any case, I don't know your situation, but; are you sure a trustee
> administer cannot also be a student administer, or a a parent
> administer also be a trustee administer.  Sound like a weird relation
> you are trying to force.  If they are mutually exclusive, fine, but
> I've never seen a setup like this.
>
> The problem is, I guess, that you may have to make the shoe fit later
> if you want to go that route.
>
> Todd

You are absolutely right...sorry for the 'mispost'.

I originally set these up as mutually exclusive groups, but now that you
point it out, it makes sense to find a way to sub-class User. Thanks for
your insight.
Ff5abdc01af5ecb5bf583174cbeac909?d=identicon&s=25 John Goetz (jgoetz)
on 2008-11-10 16:10
Brian Candler wrote:
> This sounds fine to me, but for an alternative approach you could
> consider Single Table Inheritance. That is, you have separate classes
> for Student, Parent, Teacher etc, but they all live in the users table,
> and can all be considered as a User. Teacher.find(...) will
> automatically be restricted to User.find(:conditions =>
> "type='Teacher'")
>
> http://ar.rubyonrails.com/classes/ActiveRecord/Base.html
>
> Scroll down to "Single table inheritance" Also, the book "Agile Web
> Development with Rails" has a section on this (but don't buy the 2nd
> edition now, wait for the 3rd edition due in December)
>
> However as already mentioned, this may be a Bad Idea. It's quite
> possible for the same person to be both a student and a parent, or a
> teacher and a trustee.
>
> Person.has_and_belongs_to_many :roles may be a better way of modelling
> this. Admittedly the SQL finders get a bit more awkward.

Thanks, Brian, for the direction. With Todd's advice also, I will
investigate how to write up the has-and-belongs-to-many relationship and
single table inheritance.
I already pre-purchased the 3rd edition of Agile Development with Rails
and have a PDF of the beta release. It's helped me a lot, but I didn't
get to that section yet. Time to hit the book again!
This topic is locked and can not be replied to.