I''m on about day 4 of my RubyOnRails journey (I''m only on
about 4
weeks of my Ruby journey too, so bare with me...)
I have the following (much-simplified-for-this-example) tables:
ORGANISATIONS:
id
name
REPRESENTATIVES:
id
name
ORGSREPS
org_id
rep_id
Each organisation may have zero or more representatives. Each
representative may represent zero or more organisations. The join
between the two tables is obviously through ORGSREPS
It seems quite a simple thing to do but I''m not sure what is the best
way to model this in RubyOnRails. Any pointers?
Thanks muchly
Glenn
On 29.3.2005, at 17:56, Glenn Smith wrote:> I''m on about day 4 of my RubyOnRails journey (I''m only on about 4 > weeks of my Ruby journey too, so bare with me...) > > I have the following (much-simplified-for-this-example) tables: > > ORGANISATIONS: > id > name > > REPRESENTATIVES: > id > name > > ORGSREPS > org_id > rep_id > > > > Each organisation may have zero or more representatives. Each > representative may represent zero or more organisations. The join > between the two tables is obviously through ORGSREPS > > > It seems quite a simple thing to do but I''m not sure what is the best > way to model this in RubyOnRails. Any pointers?http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ ClassMethods.html#M000398 class Organisation ... has_and_belongs_to :representative end And the same for the rep class. If you can''t change the table/field names in your db you need to use some additional attributes in your code (like ''foreign_key'', ''join_table''). See the linked docs for more info. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi Glenn, Look at the has_and_belongs_to_many relationship--it''s designed exactly for this case. You''ll need to adjust the table names and ids to the ActiveRecord style to make things as automatic as possible. Dave On 3/29/05 7:56 AM, "Glenn Smith" <glenn.rails-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m on about day 4 of my RubyOnRails journey (I''m only on about 4 > weeks of my Ruby journey too, so bare with me...) > > I have the following (much-simplified-for-this-example) tables: > > ORGANISATIONS: > id > name > > REPRESENTATIVES: > id > name > > ORGSREPS > org_id > rep_id > > > > Each organisation may have zero or more representatives. Each > representative may represent zero or more organisations. The join > between the two tables is obviously through ORGSREPS > > > It seems quite a simple thing to do but I''m not sure what is the best > way to model this in RubyOnRails. Any pointers? > > > Thanks muchly > > > Glenn > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >