Topic: Different User types, same search results question ????

Hey Guys,

Wondering if anyone can help me with this little chesnut?? smile

I am developing an app that has two different user types, lets say, student and teacher.

A teacher or a student can both create "a group" and a regular user can then search through all of these groups together. So there is no real difference between the two as far as a regular user can see.

Is it best practise to separate this into two tables so I would have a student_group table and a teacher_group table?

My question is how do you group the two together so the two can be displayed in search results?

Cheers

Stuart

Re: Different User types, same search results question ????

not sure if im heading down the wrong road with http://github.com/tsmango/union ??

Last edited by stuartchaney (2010-06-29 13:59:52)

Re: Different User types, same search results question ????

STI (Single Table Inheritance) is your friend

Add a column of type string to your user table

You din't need to change anything in your user model just do what you would normally do.
class User < ActiveRecord::Base
# This class will work with all users regardless of type
end

Create a new model (Manually create a new file rather than use the generators coz you don't wanbt a new table just a model file

your_app_root/models/student.rb

class Student < User
# Any records created using this class will will automatically have the type of the user set to Student
end

Do the same for the Teacher

Just use the student class or the teacher class or the user class depending on what you are wanting.
It really IS that simple.

e.g.
Student.all will return all users from the users table where type is set to Student.

It's totally automatic. Some would say magic
The only caveat is the a student can not also be a teacher

Last edited by jamesw (2010-06-29 14:27:07)

When your head is hurting from trying to solve a problem, stop standing on it.
Then when you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Different User types, same search results question ????

Cheers Dude! Did some reading and looks to be the right fit. thanks again

Re: Different User types, same search results question ????

Was just thinking smile

If STI lends itself to having null values throughout the table, would it be ok if I were to have just a student_id and a teacher_id column in the groups table? as this is the only difference between the two I need.

I also had a go at the type method.., added type to groups, and then created inherited models, teacher_groups and student_groups. They then showed up with type="TeacherGroup" in the Group table. I am really looking for the teacher id or the student id.

Any ideas? Is polymorphic association the way to go?

Thanks for the help again!

Stuart

Last edited by stuartchaney (2010-06-30 08:17:41)

Re: Different User types, same search results question ????

Hi Stuart,
Well, you could do that but you'd loose the magick of STI

You don't need a type in the groups table, just the user table
you just need to sort out the model relationships

class User ...
...
 has_many => :groups
...
end

class Student ...
...
 has_many => :groups, :foreign_key => 'user_id'
...
end

class Teacher ...
...
 has_many => :groups, :foreign_key => 'user_id'
...
end

class Group ...
...
  belongs_to => :user
  belongs_to => :student, :foreign_key => 'user_id'
  belongs_to => :teacher, :foreign_key => 'user_id'
...
end

then you can do things like
student.groups
teacher.groups
group.user
group.student unless group.student.blank?

etc...

When your head is hurting from trying to solve a problem, stop standing on it.
Then when you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)