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

**URL:** <https://www.ruby-forum.com/t/how-do-i-model-this-in-rails-put-down-java-equivalent-design/169691>\
**Category:** Rails\
**Created:** [June 7, 2009, 4:30am UTC](https://www.ruby-forum.com/t/how-do-i-model-this-in-rails-put-down-java-equivalent-design/169691 "2009-06-07T04:30:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ritvvij](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbce88/32.png) [@Ritvvij](https://www.ruby-forum.com/u/Ritvvij)\
**Post date:** [June 7, 2009, 4:30am UTC](https://www.ruby-forum.com/t/how-do-i-model-this-in-rails-put-down-java-equivalent-design/169691/1 "2009-06-07T04:30:11Z")

</div>

Hello guys,

Need help!! Finding it hard to model the ‘model’ 😛 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

---

<div class="post-metadata">

**Author:** ![Ritvvij](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbce88/32.png) [@Ritvvij](https://www.ruby-forum.com/u/Ritvvij)\
**Post date:** [June 7, 2009, 9:13am UTC](https://www.ruby-forum.com/t/how-do-i-model-this-in-rails-put-down-java-equivalent-design/169691/2 "2009-06-07T09:13:50Z")

</div>

On Jun 7, 3:29 am, Ritvvij [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Ritvvij](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbce88/32.png) [@Ritvvij](https://www.ruby-forum.com/u/Ritvvij)\
**Post date:** [June 7, 2009, 5:29pm UTC](https://www.ruby-forum.com/t/how-do-i-model-this-in-rails-put-down-java-equivalent-design/169691/3 "2009-06-07T17:29:16Z")

</div>

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](http://www.railsenvy.com/2007/8/8/activerecord-tutorial)

---

<div class="post-metadata">

**Author:** ![Ritvvij](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbce88/32.png) [@Ritvvij](https://www.ruby-forum.com/u/Ritvvij)\
**Post date:** [June 8, 2009, 6:42pm UTC](https://www.ruby-forum.com/t/how-do-i-model-this-in-rails-put-down-java-equivalent-design/169691/4 "2009-06-08T18:42:57Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
