# Design advice for newbie

**URL:** <https://www.ruby-forum.com/t/design-advice-for-newbie/152927>\
**Category:** Ruby\
**Created:** [November 10, 2008, 12:19am UTC](https://www.ruby-forum.com/t/design-advice-for-newbie/152927 "2008-11-10T00:19:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [November 10, 2008, 12:19am UTC](https://www.ruby-forum.com/t/design-advice-for-newbie/152927/1 "2008-11-10T00:19:54Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [November 10, 2008, 7:49am UTC](https://www.ruby-forum.com/t/design-advice-for-newbie/152927/2 "2008-11-10T07:49:13Z")

</div>

On Sun, Nov 9, 2008 at 5:17 PM, John G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [November 10, 2008, 9:48am UTC](https://www.ruby-forum.com/t/design-advice-for-newbie/152927/3 "2008-11-10T09:48:08Z")

</div>

John G. 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](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.

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [November 10, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/design-advice-for-newbie/152927/4 "2008-11-10T16:10:53Z")

</div>

Brian C. 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’”)
> 
> [Peak Obsession](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!

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [November 10, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/design-advice-for-newbie/152927/5 "2008-11-10T16:05:14Z")

</div>

Todd B. wrote:

> On Sun, Nov 9, 2008 at 5:17 PM, John G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
