Topic: two moders for one table?

Let's say i have a user table and i want to interact with that table from two different controllers, does it make sense to have 2 (or more) models for the same table?

Re: two moders for one table?

You can freely manage the same model from different controllers.  The only time you'd want to have multiple models from the same table is if you had slightly different types of models.

For example: you could have both Admins and Users in your users table.  This makes sense because they might share a lot of stuff (password management, profiles,etc.) but they are also different in a couple of keys ways (Admins get to do more).  In this kind of situation you'd want two different model classes.

It's actually really common though that a single model is handled by multiple controllers.

Re: two moders for one table?

thank you danger.

i wanted to know what's the common way of handling things out there.

Re: two moders for one table?

It might make sense to use something like single table inheritance. For example, let's say you have a "people" table, but you're keeping track of different types of people, like Employees and Customers. If you have a "type" field in your table to set the type of person you're working with you can subclass like this:

class Person < ActiveRecord::Base
end

class Customer < Person
end

class Employee < Person
end


Put functionality that's common to both Customers and Employees in the Person class, and specific functionality in the Employee or Customer classes. Then you can create a CustomersController and an EmployeesController.

vinnie - rails forum admin

Re: two moders for one table?

vin wrote:

Put functionality that's common to both Customers and Employees in the Person class, and specific functionality in the Employee or Customer classes. Then you can create a CustomersController and an EmployeesController.

nice. thanks.

nice board by the way. very helpful.

I've learned a lot from it and I'm planning to start helping newbies as soon as I'm good enough.

Last edited by allesklar (2006-08-20 10:18:47)

Re: two moders for one table?

allesklar wrote:

I've learned a lot from it and I'm planning to start helping newbies as soon as I'm good enough.

There's no rush.  Everybody here had a day, not too long ago, when we had no clue what we were doing.

Re: two moders for one table?

danger wrote:

There's no rush.  Everybody here had a day, not too long ago, when we had no clue what we were doing.

And thanks to Rails, it was only one day.

Re: two moders for one table?

Just tip: if you use a model in a controller with a different name you get to use the
  model :model_name
declaration in the class definition to pull in what's necessary...
   - Thorsten