Hey all, I''ve run into what seems to be a sticky problem. There might be a simple solution, but I haven''t been able to find it. I have one table that refers to another table three times. I want to take advantage of Rails'' association stuff for all three, but I''m just not sure how. What I''ve got is this: each Ticket can be added, edited, and approved by a User: tickets ------- id int unsigned auto_increment, submitter_id int unsigned, editor_id int unsigned, approvedby_id int unsigned, ... users ----- id int unsigned auto_increment, name varchar.... Is there any way I can set up the Ticket model to properly treat all those associations? The only thing I can come up with is to make Submitter, Editor, and Approver different types of User, and set up the associations that way, but that seems hacky. Please tell me there''s a better way! Thanks, Ben
On 6/1/05, Ben Bleything <ben-TGHtUsa5cOzMFIMGWPqnnw@public.gmane.org> wrote:> Hey all, > > I''ve run into what seems to be a sticky problem. There might be a > simple solution, but I haven''t been able to find it. > > I have one table that refers to another table three times. I want to > take advantage of Rails'' association stuff for all three, but I''m just > not sure how. > > What I''ve got is this: each Ticket can be added, edited, and approved by > a User: > > tickets > ------- > id int unsigned auto_increment, > submitter_id int unsigned, > editor_id int unsigned, > approvedby_id int unsigned, > ... > > users > ----- > id int unsigned auto_increment, > name varchar.... > > Is there any way I can set up the Ticket model to properly treat all > those associations? > > The only thing I can come up with is to make Submitter, Editor, and > Approver different types of User, and set up the associations that way, > but that seems hacky. Please tell me there''s a better way!Yeah, that''s *definitely* the wrong way to go. What you''re looking for is :class_name class Ticket < ActiveRecord::Base has_one :submitter, :class_name=>"User" has_one :editor, :class_name => "User" etc> Thanks, > Ben > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
On 31.5.2005, at 22:52, Ben Bleything wrote:> Hey all, > > I''ve run into what seems to be a sticky problem. There might be a > simple solution, but I haven''t been able to find it. > > I have one table that refers to another table three times. I want to > take advantage of Rails'' association stuff for all three, but I''m just > not sure how. > > What I''ve got is this: each Ticket can be added, edited, and approved > by > a User: > > tickets > ------- > id int unsigned auto_increment, > submitter_id int unsigned, > editor_id int unsigned, > approvedby_id int unsigned, > ... > > users > ----- > id int unsigned auto_increment, > name varchar.... > > Is there any way I can set up the Ticket model to properly treat all > those associations? > > The only thing I can come up with is to make Submitter, Editor, and > Approver different types of User, and set up the associations that way, > but that seems hacky. Please tell me there''s a better way!Yes, there is! You can define as many belongs_to associations to a single class as you want to: Ticket belongs_to :submitter, :class_name => "User", :foreign_key => "submitter_id" belongs_to :editor, :class_name => "User", :foreign_key => "editor_id" belongs_to :approver, :class_name => "User", :foreign_key => "approvedby_id" After that you have ticket.submitter, ticket.editor and ticket.approver at hand. Consult the api docs (http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ ClassMethods.html#M000435) for more info. //jarkko> > Thanks, > Ben > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails