# Authentication for 2 models

**URL:** <https://www.ruby-forum.com/t/authentication-for-2-models/104079>\
**Category:** Rails\
**Created:** [May 14, 2007, 4:45am UTC](https://www.ruby-forum.com/t/authentication-for-2-models/104079 "2007-05-14T04:45:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [May 14, 2007, 4:45am UTC](https://www.ruby-forum.com/t/authentication-for-2-models/104079/1 "2007-05-14T04:45:55Z")

</div>

I’ve got 2 models: School and Student

I’m using restful authentication for school so when someone signs up  
they are routed to schools/new  
The schools table has info for an admin, but I also need  
authentication for students. Here is what I was thinking would be the  
best way to do this:

#1 Combine admin and students into User model and have an admin?  
boolean column. So schools would be info about the school, with no  
admin info.

#2 When logging in, if the user is a student they would check the  
student checkbox so the query can be against students, rather than  
schools. I believe this is basically like having 2 authentication  
systems which seems kind of complicated.

Which way would you guys do this?

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [May 14, 2007, 8:21am UTC](https://www.ruby-forum.com/t/authentication-for-2-models/104079/2 "2007-05-14T08:21:07Z")

</div>

jko170 wrote:

> I’ve got 2 models: School and Student
> 
> I’m using restful authentication for school so when someone signs up  
> they are routed to schools/new  
> The schools table has info for an admin, but I also need  
> authentication for students. Here is what I was thinking would be the  
> best way to do this:
> 
> #1 Combine admin and students into User model and have an admin?  
> boolean column. So schools would be info about the school, with no  
> admin info.
> 
> #2 When logging in, if the user is a student they would check the  
> student checkbox so the query can be against students, rather than  
> schools. I believe this is basically like having 2 authentication  
> systems which seems kind of complicated.
> 
> Which way would you guys do this?

The best way i would suggest you is to identify all the entities for  
your requirement. Looks like you may need staff model, again teaching  
and non-teaching staffs.

For now with the above two models, instead of using a boolean column, by  
default have an entry in your Student tables with id = 0, that  
represents the admin. Any data retrieval will be \> 0 “where clause”.

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [May 14, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/authentication-for-2-models/104079/3 "2007-05-14T20:17:44Z")

</div>

Here’s an approach I’ve used before… you have a user model and a  
role  
model and then you link the two.

class Role \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users  
end

class User \< ActiveRecord::Base

has\_and\_belongs\_to\_many :roles

def has\_role(role)  
self.roles.detect(|r| r.name == role}  
end

def is\_student?  
self.has\_role?(“student”)  
end

def is\_admin?  
self.has\_role?(“admin”)  
end

end

Role.create :name=\>“admin”  
Role.create :name=\>“student”

user = User.create :last\_name=\>“Simpson”, :first\_name=\>“Homer”,  
:password=\>“1234”, :password\_confirmation=\>“1234”, :email=\>"  
removed\_email\_address@domain.invalid", :login=\>“homer”

user.roles \<\< Role.find\_by\_name “admin”  
user.roles \<\< Role.find\_by\_name “student”

Makes it really easy then, cos in your views, if you use restful\_auth or  
acts\_as\_authenticated, your current\_user will have a .is\_admin? on it.

\<% if current\_user.is\_admin? %\>  
… show stuff for admins

\<% end %\>

Hope that helps.

-Brian

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [May 14, 2007, 9:02pm UTC](https://www.ruby-forum.com/t/authentication-for-2-models/104079/4 "2007-05-14T21:02:49Z")

</div>

I’ll give this a try, thanks a lot!
