views:

112

answers:

2

I'm just learning how to program in Ruby on Rails, and I'm really impressed by how much work the Ruby on Rails framework does for you. I have the following two classes:

Object
--------
object_id
description

ObjectGroup
--------
group_id 
description

The idea is that I have some objects, and I'd like for the users to be able to define groups of Objects. Both Object rows and ObjectGroup rows these have their own identifiers and associated data, that's very intuitive based on the published documents on the web. My question is - how do I store information on the Objects that make up an ObjectGroup and make best use of the Ruby on Rails framework? The obvious way to do this in SQL was to make use of a third table that stored entries of the following form:

ObjectGroupEntry
--------
object_id
group_id

Each entry in the ObjectGroupEntry table represents an Object that is part of an ObjectGroup. Is it my responsibility to create this table manually and teach the classes that interact with Objects and ObjectGroups how to interact with it? Or does Ruby on Rails provide some magic to do this that I haven't found yet?

I could do this myself with what I know, but I'm still learning Ruby on Rails and I'd like to learn the Right Way (TM) to do this.

+3  A: 

With Rails it's a bit of both. You create the table using a migration (or other method) with the normal naming convention, and then in the models for Object and ObjectGroup you would put in the line:

==Object==
has_and_belongs_to_many :object_groups

==ObjectGroup==
has_and_belongs_to_many :objects

As long as you don't have any extra info in the ObjectObjectGroup table then this is all you need. Otherwise you would need a new model but could get the same behaviour like so:

==Object==
has_many :object_object_groups
has_many :object_groups, :through => :object_object_groups

==ObjectObjectGroup==
belongs_to :object
belongs_to :object_group

==ObjectGroup==
has_many :object_object_groups
has_many :objects, :through => :object_object_groups

indicating the has_many relationship with the :through attribute to get a many-to-many relationship and allowing access to other information on the relationship as well.

workmad3
What's the normal naming convention? Can you re-name ObjectObjectGroup to ObjectGroupList? Sorry, I'm easily confused. :)
James Thompson
http://itsignals.cascadia.com.au/?p=7 for naming conventions. You can provide your own tables and models etc and override the conventions, but this is (intentionally) more work than just going with the conventions in the first place and I've never bothered remembering how to do it :)
workmad3
Its has_and_belongs_to_many and has_many :through =>
railsninja
Oh yes, my mistake, corrected now :)
workmad3
+1  A: 

I think that you will find it is

has_many :object_object_groups
has_many :objects, :though => :through => :object_object_groups

rather than what workmad3 has stated. All the other information provided is right though, it's just a syntax error.

railsninja