Forum: Ruby on Rails should the polymorfic modeling be used?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D24d04e0be3239b2ced36b796a2227b9?d=identicon&s=25 Nyc, Petr (Guest)
on 2009-01-24 13:26
(Received via mailing list)
Hi experts,

I am new to rails and am struggling for several days with a following
model
(Rails 2.2.2).

People are submitting requests, each request has one submitter, one
approver, several reviewers and several responsible people.

I was able to get this model working:

class Person < ActiveRecord::Base
  has_many :Requests
end

class Request < ActiveRecord::Base
  belongs_to :Approver, :class_name => "Person", :foreign_key =>
"approver_id"
  belongs_to :Submitter, :class_name => "Person", :foreign_key =>
"submitter_id"
end


But I am really stuck in creating HABTM relationship between
requests<->reviewers and requests<->responsible_people.. I do not want
to
create separate tables for each group of people as one is sometimes
submitter/approver and sometimes belongs to reviewers/responsible.

Can you someone point me the right direction?
Thanks!
JetPac
A91bd6cef23eb3516245a092e196c4da?d=identicon&s=25 Maurício Linhares (mauricio)
on 2009-01-24 15:15
(Received via mailing list)
Here's how your code should be:

class Person < ActiveRecord::Base
  has_many :requests_as_approver, :class_name => 'Request',
:foreign_key => 'approver_id'
  has_many :requests_as_submitter, :class_name => 'Request',
:foreign_key => 'approver_id'
end

class Request < ActiveRecord::Base
  belongs_to :approver, :class_name => "Person", :foreign_key =>
"approver_id"
  belongs_to :submitter, :class_name => "Person", :foreign_key =>
"submitter_id"
end

I can't see the need for a N:N association here (and thus as
has_and_belongs_to_many association).

-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)
This topic is locked and can not be replied to.