Newbie many to many relationship within the same class

Hi,

Im a real newbie to ruby, but I have a bit of a background in php/mysql.
Ive been assigned work on a low profile, low priority project to help me
learn. So a lot of the tables are already defined and I probably
shouldnt change them at this stage.

I have a table of users:
Name, loginname, department_number(and more) this is already modeled
and working in the app

I have a table of departments
department_admin_loginname, department_number(and more) - this is an
existing table in a different database on the same ms sql server. It is
not a part of the app and is used by several other apps, so I really
cant alter it.

Departments can have more than one admin, and a user can be an admin of
more than one department. A user may belong to multiple departments,
thus having multiple department admins. I dont care what department you
are in.

All I need to know is when a user logs in, is the user an admin and who
are people he or she is an admin for. If the user is not an admin,
show who your admins are.

My first instinct was to create a view that joined up a few tables in a
couple of different databasesd order to produce a lookup table:
admin_loginname, user_loginname

When a user logs in, fire two quick queries: select user_loginname from
jointable where admin_loginname=myname and select admin_loginname from
jointable where user_loginname=byname. Bada-boom, bada-bing 4 lines of
php and Ive got your info ready to go. But I know how ruby people feel
about php. :wink:

I think I understand how I would use has_many and belongs to if I were
connecting 2 different modeled objects. Customers and Orders come up a
lot in the examples. But in this case, a user can be associated with
many users based on that lookup table. The lookup table isnt really a
class that should be modeled, neither are any of the tables in the join
to produce the view.

Any ideas or pointers to examples?

Thanks!

Subject: newbie many to many relationship within the same class
Date: Fri 12 Sep 14 06:39:56PM +0000

Quoting Ramsey, Robert L ([email protected]):

All I need to know is when a user logs in, is the user an admin and
who are people he or she is an admin for. If the user is not an
admin, show who your admins are.

If I were you, since you express a wish to learn Ruby, I would try to
solve the problem in a procedural way - i.e. define a few methods, and
inside them solve your problem in a clear, sequential way. You should
not target speed or terseness, but clarity. You should postpone the
bada-boom bada-bing to a later moment.

For example, I can think about adding an ‘admins’ method in my user
class, which would return an array of 1 or more user class
instances - possibly including the user him/herself if s/he is an
admin. But there are possibly infinite ways to solve your problem.

Carlo