How do i model this in Rails? (put down java equivalent design)

Hello guys,

Need help!! Finding it hard to model the ‘model’ :stuck_out_tongue: within the given
rails associations.

In java terms, my requirements are…

There are 3 entities - module, chapter and activity.

Chapter entity is a specilization of Module entity (inheritence)
Activity entity is a specialization of Module entity (inheritence)
And the tables would have looked like:

  1. Module table = Module_Id, Entity_type, …
  2. Chapter = Chapter_id, …
  3. Activity = Activity_id, …

Hence, if I create a chapter then the tables would be populated as:
Module table = Module_Id, Entity_type, … = values ( 100,
chapter , … )
Chapter table = chapter_id, … = values ( 100, … )
Basically, the ids of chapter and module would be same and module
table would store the type.

Now, if I create a activity then the tables would be populated as:
Module table = Module_Id, Entity_type, … = values ( 101,
activity , … )
Activity table = activity_id, … = values ( 100, … )
Basically, the ids of activity and module would be same and module
table would store the type.

Can you please guide me how I can model it similarly in rails 2.3.2?

Thanks in advance
Ritvvij Parrilkh

On Jun 7, 3:29 am, Ritvvij [email protected] wrote:

Activity entity is a specialization of Module entity (inheritence)
table would store the type.

That wouldn’t be the rails way of doing it. In rails you’d probably
either use single table inheritance or a polymorphic association.

Fred

Does seem to be a bit of repetition in your description but it seems
straightforward.

You have a Module which has an Entity_type column

Each Module has many Chapters where Chapters has a column named
module_id.

Each Chapter has many Activities with similar foreign key constraints
or
You have one Activities table, in which case you don’t specify a
relationship.

Once you set up the has_many and belongs_to pairs, Rails provides the
methods to interact with the data.

Checkout some ActiveRecord tutorials like this screencast -
http://www.railsenvy.com/2007/8/8/activerecord-tutorial

I’d also chime in that if you create a model named ‘Module’, Rails
will most likely break in really interesting ways, as Module is a
reserved word in Ruby…

–Matt J.

On Jun 7, 3:12 am, Frederick C. [email protected]