Hey guys, I''m creating a web app with some fairly complex user management. I have a table of users and a table of members. Basically, a user is someone who signs up for an account. In their account they can create multiple projects and administer them. For each project they can create members who hav access to one of their specified projects. So lets say user_1 creates project_1 and project_2. My application automatically adds him as a member to each project in the members table which has the project_id as a foreign key. So my users table contains the following: user_1 My members table however contains: user_1 project_1 user_1 project_2 When a user adds new users to their project I want to ensure that the member has a unique username, but this user name only has to be unique in the context of it''s associated project. So if my members table contains the following: user_1 project_1 user_1 project_2 user_a project_1 user_a project_2 user_b project_1 user_c project_1 My administrator should be able to successfully: add user_c project_2 However they should get an error if they attempt to: add user_a project_2 This is becuase project_2 already has a user_a but user_c only exists in project_1 so it can be allowed in project_2. Does this all make sense? Is there a parameter for validates_uniqueness_of that can accommodate a special request like this or do you have to do it manually? Thanks for all your help. This list is fantastic! - Jim Jeffers
I would either write a custom validate method to check for an existing member, or use single table inheritance and make user a subclass of member. Pat On 8/5/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote:> Hey guys, > > I''m creating a web app with some fairly complex user management. I > have a table of users and a table of members. > > Basically, a user is someone who signs up for an account. In their > account they can create multiple projects and administer them. For > each project they can create members who hav access to one of their > specified projects. > > So lets say user_1 creates project_1 and project_2. > > My application automatically adds him as a member to each project in > the members table which has the project_id as a foreign key. > > So my users table contains the following: > user_1 > > My members table however contains: > user_1 project_1 > user_1 project_2 > > When a user adds new users to their project I want to ensure that the > member has a unique username, but this user name only has to be > unique in the context of it''s associated project. So if my members > table contains the following: > > user_1 project_1 > user_1 project_2 > user_a project_1 > user_a project_2 > user_b project_1 > user_c project_1 > > My administrator should be able to successfully: > add user_c project_2 > > However they should get an error if they attempt to: > add user_a project_2 > > This is becuase project_2 already has a user_a but user_c only exists > in project_1 so it can be allowed in project_2. > > Does this all make sense? Is there a parameter for > validates_uniqueness_of that can accommodate a special request like > this or do you have to do it manually? > > Thanks for all your help. This list is fantastic! > > - Jim Jeffers > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
fwiw in some cases I would do a custom validate. In this case though I''d probably use STI because a user seems like a logical extension of a member. On 8/5/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I would either write a custom validate method to check for an existing > member, or use single table inheritance and make user a subclass of > member. > > Pat > > > > On 8/5/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote: > > Hey guys, > > > > I''m creating a web app with some fairly complex user management. I > > have a table of users and a table of members. > > > > Basically, a user is someone who signs up for an account. In their > > account they can create multiple projects and administer them. For > > each project they can create members who hav access to one of their > > specified projects. > > > > So lets say user_1 creates project_1 and project_2. > > > > My application automatically adds him as a member to each project in > > the members table which has the project_id as a foreign key. > > > > So my users table contains the following: > > user_1 > > > > My members table however contains: > > user_1 project_1 > > user_1 project_2 > > > > When a user adds new users to their project I want to ensure that the > > member has a unique username, but this user name only has to be > > unique in the context of it''s associated project. So if my members > > table contains the following: > > > > user_1 project_1 > > user_1 project_2 > > user_a project_1 > > user_a project_2 > > user_b project_1 > > user_c project_1 > > > > My administrator should be able to successfully: > > add user_c project_2 > > > > However they should get an error if they attempt to: > > add user_a project_2 > > > > This is becuase project_2 already has a user_a but user_c only exists > > in project_1 so it can be allowed in project_2. > > > > Does this all make sense? Is there a parameter for > > validates_uniqueness_of that can accommodate a special request like > > this or do you have to do it manually? > > > > Thanks for all your help. This list is fantastic! > > > > - Jim Jeffers > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
What do you mean by a single table inheritance? I already have a separate table for users and members.. can you go into more detail? On Aug 5, 2005, at 8:58 PM, Pat Maddox wrote:> fwiw in some cases I would do a custom validate. In this case though > I''d probably use STI because a user seems like a logical extension of > a member. > > > > On 8/5/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> I would either write a custom validate method to check for an >> existing >> member, or use single table inheritance and make user a subclass of >> member. >> >> Pat >> >> >> >> On 8/5/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote: >> >> >>> Hey guys, >>> >>> I''m creating a web app with some fairly complex user management. I >>> have a table of users and a table of members. >>> >>> Basically, a user is someone who signs up for an account. In their >>> account they can create multiple projects and administer them. For >>> each project they can create members who hav access to one of their >>> specified projects. >>> >>> So lets say user_1 creates project_1 and project_2. >>> >>> My application automatically adds him as a member to each project in >>> the members table which has the project_id as a foreign key. >>> >>> So my users table contains the following: >>> user_1 >>> >>> My members table however contains: >>> user_1 project_1 >>> user_1 project_2 >>> >>> When a user adds new users to their project I want to ensure that >>> the >>> member has a unique username, but this user name only has to be >>> unique in the context of it''s associated project. So if my members >>> table contains the following: >>> >>> user_1 project_1 >>> user_1 project_2 >>> user_a project_1 >>> user_a project_2 >>> user_b project_1 >>> user_c project_1 >>> >>> My administrator should be able to successfully: >>> add user_c project_2 >>> >>> However they should get an error if they attempt to: >>> add user_a project_2 >>> >>> This is becuase project_2 already has a user_a but user_c only >>> exists >>> in project_1 so it can be allowed in project_2. >>> >>> Does this all make sense? Is there a parameter for >>> validates_uniqueness_of that can accommodate a special request like >>> this or do you have to do it manually? >>> >>> Thanks for all your help. This list is fantastic! >>> >>> - Jim Jeffers >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >>> >> >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >
Basically what it is is a way of specifying that a user is simply a more specific member. In your Ruby code, a user would extend a member: class Member < ActiveRecord::Base class User < Member This is because a User is the same as a member, just with some more privileges, and probably a few more attributes. So in your DB you would include fields for all the attributes a user can have, and in addition add a type column, which Rails automatically fills to determine whether a someone is a user or a member. This is in one table though...you only have one table called members. There''s a bit of info in the wiki, and the AR::Base docs cover it a bit. I''d recommend checking out section 15.3 of the Agile Development book. Pat On 8/5/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote:> What do you mean by a single table inheritance? I already have a > separate table for users and members.. can you go into more detail? > > On Aug 5, 2005, at 8:58 PM, Pat Maddox wrote: > > > > fwiw in some cases I would do a custom validate. In this case though > > I''d probably use STI because a user seems like a logical extension of > > a member. > > > > > > > > On 8/5/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > >> I would either write a custom validate method to check for an > >> existing > >> member, or use single table inheritance and make user a subclass of > >> member. > >> > >> Pat > >> > >> > >> > >> On 8/5/05, Jim Jeffers <rails-u78NUfcIof50Y1uG8So6J1aTQe2KTcn/@public.gmane.org> wrote: > >> > >> > >>> Hey guys, > >>> > >>> I''m creating a web app with some fairly complex user management. I > >>> have a table of users and a table of members. > >>> > >>> Basically, a user is someone who signs up for an account. In their > >>> account they can create multiple projects and administer them. For > >>> each project they can create members who hav access to one of their > >>> specified projects. > >>> > >>> So lets say user_1 creates project_1 and project_2. > >>> > >>> My application automatically adds him as a member to each project in > >>> the members table which has the project_id as a foreign key. > >>> > >>> So my users table contains the following: > >>> user_1 > >>> > >>> My members table however contains: > >>> user_1 project_1 > >>> user_1 project_2 > >>> > >>> When a user adds new users to their project I want to ensure that > >>> the > >>> member has a unique username, but this user name only has to be > >>> unique in the context of it''s associated project. So if my members > >>> table contains the following: > >>> > >>> user_1 project_1 > >>> user_1 project_2 > >>> user_a project_1 > >>> user_a project_2 > >>> user_b project_1 > >>> user_c project_1 > >>> > >>> My administrator should be able to successfully: > >>> add user_c project_2 > >>> > >>> However they should get an error if they attempt to: > >>> add user_a project_2 > >>> > >>> This is becuase project_2 already has a user_a but user_c only > >>> exists > >>> in project_1 so it can be allowed in project_2. > >>> > >>> Does this all make sense? Is there a parameter for > >>> validates_uniqueness_of that can accommodate a special request like > >>> this or do you have to do it manually? > >>> > >>> Thanks for all your help. This list is fantastic! > >>> > >>> - Jim Jeffers > >>> > >>> _______________________________________________ > >>> Rails mailing list > >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >>> http://lists.rubyonrails.org/mailman/listinfo/rails > >>> > >>> > >>> > >> > >> > >> > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Jim, Unless I am missing something here, the solution is very simple: In your user model: validates_uniqueness_of :user_id, :scope => "project_id" Should do what you want: make sure that each user is only entered in a project once. Think of the statement as saying "For all the records that have the same (project_id) verify that the (user_id) only exists once." Something like that. It seems to me, all together, that you might need a little redesign in this user management schema. If you would be interested, let me know. -Jeff Jim Jeffers wrote:> What do you mean by a single table inheritance? I already have a > separate table for users and members.. can you go into more detail? > > On Aug 5, 2005, at 8:58 PM, Pat Maddox wrote: >-- Jeff Casimir Developer, CommonText Literacy Project: http://www.commontext.com Teacher, César Chávez PCHS: http://www.cesarchavezhs.org