# Different Usermodels with Authlogic + Declarative Authorizat

**URL:** <https://www.ruby-forum.com/t/different-usermodels-with-authlogic-declarative-authorizat/181066>\
**Category:** Rails\
**Created:** [January 5, 2010, 12:25am UTC](https://www.ruby-forum.com/t/different-usermodels-with-authlogic-declarative-authorizat/181066 "2010-01-05T00:25:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hatra-e](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a4c791/32.png) [@hatra-e](https://www.ruby-forum.com/u/hatra-e)\
**Post date:** [January 5, 2010, 12:25am UTC](https://www.ruby-forum.com/t/different-usermodels-with-authlogic-declarative-authorizat/181066/1 "2010-01-05T00:25:26Z")

</div>

Hi there,  
I’m working on a Rails App, in which I use the gems Authlogic and  
Declarative Authorization to put Users into different roles.

But because the roles are very different from each other, I thought it  
would be better, if every usertype gets its own databasetable / model.

The model Student has for example the attributes: Name, Birthday,  
E-Mail, Grades, Schools, Lessions, Awards and many many more.  
But the model Teacher has only Name, Birthday, E-Mail  
The Admin model gets just Name and E-Mail.

Now my Problem:  
Declarative Authorization and many other authorization-systems, that I  
looked in, only working with one Usermodel, in which different roles get  
applied.  
This would mean, that even Teachers and Admins would have the Attributes  
of the Students (but with no Values, which makes validations difficult).  
I would like to know, how I can separate Students, Teachers and Admins  
into different user models and get it to work with Authlogic and  
Declarative Authorization.

I would like to use commands like Teacher.find\_by\_name(‘Bob’) or  
current\_user.is\_student?

Do you know the answer and can you show me some code examples?

---

<div class="post-metadata">

**Author:** ![hatra-e](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a4c791/32.png) [@hatra-e](https://www.ruby-forum.com/u/hatra-e)\
**Post date:** [January 5, 2010, 1:37am UTC](https://www.ruby-forum.com/t/different-usermodels-with-authlogic-declarative-authorizat/181066/2 "2010-01-05T01:37:45Z")

</div>

Have you thought about using Single Table inheritance (STI) for the  
User model? A Google search of “rails single table inheritance” will  
yield some good tutorials.

One of the big reasons for not using STI is if your models are very  
different with regards to attributes of the sub classes that get  
stored in the Users table but from your description below they don’t  
seem that different if you disregard the associations (Grades,  
Schools, Lessons, etc) because the associations will be implemented  
with either linking tables (has\_and\_belongs\_to\_many or  
has\_many :through) or foreign keys on the Grades, Schools, Lessons,  
etc tables (has\_many on User end and belongs\_to on the other end) and  
not in columns in the Users table.

Maybe you could check out implementing STI and let me know if it works  
out…

I have never used STI with Declarative Authorization but I’m guessing  
that since you’ll have one base class (User) it should work out since  
I believe that you can define the roles on the base class.

I am using STI on a project with a User model and it works well.

Mike

On Jan 4, 4:25 pm, Ruven P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The Admin model gets just Name and E-Mail.
> 
> I would like to use commands like Teacher.find\_by\_name(‘Bob’) or  
> current\_user.is\_student?
> 
> ## Do you know the answer and can you show me some code examples?
> 
> Posted viahttp://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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![hatra-e](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a4c791/32.png) [@hatra-e](https://www.ruby-forum.com/u/hatra-e)\
**Post date:** [January 6, 2010, 8:31pm UTC](https://www.ruby-forum.com/t/different-usermodels-with-authlogic-declarative-authorizat/181066/3 "2010-01-06T20:31:48Z")

</div>

I’m going with Devise now, since it can make different models  
“authenticatable” 🙂

Mike Gehard wrote:

> Maybe you could check out implementing STI and let me know if it works  
> out…
