Kevin Fullerton
2005-Jun-08 15:20 UTC
Joins - belongs_to and has_and_belongs_to_many in same class?
Hi,
Probably a newbie question, but I''ve got the following setup for a
Rails
application at the moment
Ruby 1.8.2, Rails 0.12.1, Debian unstable, Apache mod_rewrite/FastCGI
Table: members
CREATE TABLE `members` (
`id` int(11) NOT NULL auto_increment,
`organisation_id` int(11) default NULL,
`username` varchar(32) NOT NULL default '''',
`password` varchar(128) NOT NULL default '''',
`displayname` varchar(255) NOT NULL default '''',
`title` varchar(255) default NULL,
`email` varchar(255) NOT NULL default '''',
`isadmin` int(11) NOT NULL default ''0'',
`comments` text,
`created_on` datetime NOT NULL default ''0000-00-00
00:00:00'',
`updated_on` datetime NOT NULL default ''0000-00-00
00:00:00'',
PRIMARY KEY (`id`),
) TYPE=InnoDB;
table: projects
CREATE TABLE `projects` (
`id` int(11) NOT NULL auto_increment,
`organisation_id` int(11) NOT NULL default ''0'',
`member_id` int(11) NOT NULL default ''0'',
`projectname` varchar(255) NOT NULL default '''',
`created_on` datetime NOT NULL default ''0000-00-00
00:00:00'',
`updated_on` datetime NOT NULL default ''0000-00-00
00:00:00'',
PRIMARY KEY (`id`)
) TYPE=InnoDB;
table: members_projects
CREATE TABLE `members_projects` (
`member_id` int(11) NOT NULL default ''0'',
`project_id` int(11) NOT NULL default ''0''
) TYPE=InnoDB;
I''ve got models/controllers/views for member and project. The problem
I''ve got is I want the member_id in projects to hold the owner, but
also
list the people who are involved in the project via the members_projects
table.
model: member
class Member < ActiveRecord::Base
validates_uniqueness_of :username
validates_uniqueness_of :email
belongs_to :organisation
has_many :project
has_and_belongs_to_many :project
def self.authenticate(username,password)
find(:first, :conditions => ["username = ? and password
?",username,password])
end
end
model: project
class Project < ActiveRecord::Base
belongs_to :organisation
belongs_to :member
has_and_belongs_to_many :member
end
This gives me an Action Controller Exception - before the
has_and_belongs_to_many were added it was working fine. Question is, can
I get it working like this with just a change to the model, or can I
rename member_id to owner_id in the table and change the model/views to
support this, or is there another way of doing it that''s better?
Cheers
Kevin
Stephan Oehlert
2005-Jun-08 15:47 UTC
Re: Joins - belongs_to and has_and_belongs_to_many in same class?
> > I''ve got models/controllers/views for member and project. The problem > I''ve got is I want the member_id in projects to hold the owner, but also > list the people who are involved in the project via the members_projects > table. > > model: member > > class Member < ActiveRecord::Base > validates_uniqueness_of :username > validates_uniqueness_of :email > belongs_to :organisation > has_many :project > has_and_belongs_to_many :project > def self.authenticate(username,password) > find(:first, :conditions => ["username = ? and password > ?",username,password]) > end > end > > model: project > > class Project < ActiveRecord::Base > belongs_to :organisation > belongs_to :member > has_and_belongs_to_many :member > end > > This gives me an Action Controller Exception - before the > has_and_belongs_to_many were added it was working fine. Question is, can > I get it working like this with just a change to the model, or can I > rename member_id to owner_id in the table and change the model/views to > support this, or is there another way of doing it that''s better? >I think you could do class Owner < Member has_many :projects end class Member habtm :projects end class Project habtm :members, :foreign_key => "project_id" belongs_to :owner, :foreign_key => "owner_id" end This would differentiate between the roles of a member and should eliminate the exception. stephan
Kevin Fullerton
2005-Jun-09 08:57 UTC
Re: Joins - belongs_to and has_and_belongs_to_many in same class?
Thanks for your help, that worked a treat! Kevin> I think you could do > > class Owner < Member > has_many :projects > end > > class Member > habtm :projects > end > > class Project > habtm :members, :foreign_key => "project_id" > belongs_to :owner, :foreign_key => "owner_id" > end > > This would differentiate between the roles of a member and should > eliminate > the exception. > > stephan > >