Hi all, I''m making a simple group application. Users can make groups and invite people to join this group. I have the following three tables now: users id int unsigned not null primary key, name varchar(64) not null default '''', password varchar(32) not null default '''' groups id int unsigned not null primary key, name varchar(64) not null default '''' groups_users group_id int unsigned not null default 0, user_id int unsigned not null default 0, primary key (group_id, user_id) (groups_users table holds group members information. users can join to many groups.) And the following two models: Class User < ActiveRecord::Base has_and_belongs_to_many :groups end Class Group < ActiveRecord::Base has_and_belongs_to_many :users end Now I want to add a column in the groups table for the group admin using user_id. How should I name this and tell Rails about the relationship? Conceptually, Group has_one User as admin, but I can''t just add a line to Group Class as [has_one :user] because it already has [has_and_belongs_to_many :users] for group members. Isn''t there any fancy Rails-like way instead of just add a admin_id column, then write the whole code for it? Maybe, making a Admin subclass from User? Then...? Thanks for your insights! --Joon
Hi in Group do something like: class Group < ActiveRecord::Base has_and_belongs_to_many :users has_one :administrator, :class_name => ''User'', :foreign_key => ''administrator_id'' end You then need an administrator_id column in your Groups class Regards, Steve On 3 Nov 2005, at 21:44, Kyungjoon Lee wrote:> Hi all, > > I''m making a simple group application. Users can make groups and > invite people to join this group. > > I have the following three tables now: > > users > id int unsigned not null primary key, > name varchar(64) not null default '''', > password varchar(32) not null default '''' > > groups > id int unsigned not null primary key, > name varchar(64) not null default '''' > > groups_users > group_id int unsigned not null default 0, > user_id int unsigned not null default 0, > primary key (group_id, user_id) > > (groups_users table holds group members information. users can join to > many groups.) > > > And the following two models: > > Class User < ActiveRecord::Base > has_and_belongs_to_many :groups > end > > Class Group < ActiveRecord::Base > has_and_belongs_to_many :users > end > > > Now I want to add a column in the groups table for the group admin > using user_id. How should I name this and tell Rails about the > relationship? Conceptually, Group has_one User as admin, but I can''t > just add a line to Group Class as [has_one :user] because it already > has [has_and_belongs_to_many :users] for group members. > > Isn''t there any fancy Rails-like way instead of just add a admin_id > column, then write the whole code for it? Maybe, making a Admin > subclass from User? Then...? > > Thanks for your insights! > > --Joon > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > lists.rubyonrails.org/mailman/listinfo/rails
I mean you''ll need an administrator_id column in your groups *table*, obviously. It''s late here. Long day. Excuses, excuses...> Hi > > in Group do something like: > > class Group < ActiveRecord::Base > has_and_belongs_to_many :users > has_one :administrator, :class_name => ''User'', :foreign_key => > ''administrator_id'' > end > > You then need an administrator_id column in your Groups class > > Regards, > Steve > > > On 3 Nov 2005, at 21:44, Kyungjoon Lee wrote: > >> Hi all, >> >> I''m making a simple group application. Users can make groups and >> invite people to join this group. >> >> I have the following three tables now: >> >> users >> id int unsigned not null primary key, >> name varchar(64) not null default '''', >> password varchar(32) not null default '''' >> >> groups >> id int unsigned not null primary key, >> name varchar(64) not null default '''' >> >> groups_users >> group_id int unsigned not null default 0, >> user_id int unsigned not null default 0, >> primary key (group_id, user_id) >> >> (groups_users table holds group members information. users can >> join to >> many groups.) >> >> >> And the following two models: >> >> Class User < ActiveRecord::Base >> has_and_belongs_to_many :groups >> end >> >> Class Group < ActiveRecord::Base >> has_and_belongs_to_many :users >> end >> >> >> Now I want to add a column in the groups table for the group admin >> using user_id. How should I name this and tell Rails about the >> relationship? Conceptually, Group has_one User as admin, but I can''t >> just add a line to Group Class as [has_one :user] because it already >> has [has_and_belongs_to_many :users] for group members. >> >> Isn''t there any fancy Rails-like way instead of just add a admin_id >> column, then write the whole code for it? Maybe, making a Admin >> subclass from User? Then...? >> >> Thanks for your insights! >> >> --Joon >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > lists.rubyonrails.org/mailman/listinfo/rails