# ActiveRecord - Data Modeling

**URL:** <https://www.ruby-forum.com/t/activerecord-data-modeling/165391>\
**Category:** Rails\
**Created:** [April 13, 2009, 4:18pm UTC](https://www.ruby-forum.com/t/activerecord-data-modeling/165391 "2009-04-13T16:18:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Celio\_Motta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@Celio\_Motta](https://www.ruby-forum.com/u/Celio_Motta)\
**Post date:** [April 13, 2009, 4:18pm UTC](https://www.ruby-forum.com/t/activerecord-data-modeling/165391/1 "2009-04-13T16:18:49Z")

</div>

Hello!

I am coming from [ASP.NET](http://ASP.NET) straight into Rails world and have read some  
books and watch many screencasts so I decided to start a project in  
Rails and this project is actually a Rails version of my [ASP.NET](http://ASP.NET) site.  
Well, by default Rails uses single PK (althogh I saw some plugins that  
allow us creating composite PK with more than one attribute) so I  
decide to stick to the idea of PK Rails uses by default. (as of my  
understanding, please correct me if I’m wrong). My entities are (some  
of them, just to start):

```
    Semester :semester
    Disciplines :code, :description, :ratio
    Classes :start_time, :end_time, :description, :teacher

```

The idea is:

```
Semester has many Disciplines offered in that semester
Disciplines has many Classes

```

Since Disciplines are like a catalog of available disciplines to be  
offered in one very semester, I didn’t want to repeat disciplines for  
each semester with “semester has\_many :disciplines” because it would  
cause the discipline code and descriptions (which is somewhat big name  
of it) to be repeated to every discipline and every semester in  
Disciplines Model like this:

In the Semester Model,  
:semester\_id =\> 1, lets say its 2009.1  
:semester\_id =\> 2, lets say its 2009.2

In the Discipline Model:  
:id = \> 1, :semester\_id =\> 1, :code =\> “ADM1004”, :description =\>  
“ADMINISTRATION FOR ENGENEERING”, :ratio =\> 2  
:id = \> 2, :semester\_id =\> 2, :code =\> “ADM1004”, :description =\>  
“ADMINISTRATION FOR ENGENEERING”, :ratio =\> 2

So the :code and :description is somewhat redundant and wasting space  
on the database and I came up with using a third model to join them  
like this:

#Semester.rb  
class Semester \< ActiveRecord::Base  
has\_many :discipline\_semesters  
has\_many :disciplines, :through =\> :discipline\_semesters  
end

#Discipline.rb  
class Discipline \< ActiveRecord::Base  
has\_many :discipline\_semesters  
has\_many :classes, :through =\> :discipline\_semesters  
end

#Class.rb  
class Class\< ActiveRecord::Base  
belongs\_to :discipline\_semesters  
end

#DisciplineSemester.rb  
class DisciplineSemester\< ActiveRecord::Base  
has\_many :classes  
belongs\_to :semester  
belongs\_to :discipline  
end

Bad thing is somewhat wierd to create new classes for a  
DisciplineSemester because it would be something like:

Semester.create!(:semester=\> 20091) - new semester  
semester = Semester.find(1) - Get it into a variable  
semester.disciplines \<\< Discipline.create!(:code=\>  
“ADM1004”, :description=\> “ADMINISTRACAO PARA ENGENHARIA”, :ratio=\>  
4) - Creates and associates a class with the Semester  
discipline = p.disciplines.find\_by\_code(“ADM1004”) - Get the  
discipline variable  
discipline.classes- returns an empty array, none yet  
discipline.classes \<\< Class.create!(:code =\> ‘33A’, :start\_time =\>  
‘13h’, :end\_time =\> ‘15h’, :description =\> ‘Especial Class For Nerds’)

- tries to create an new classe for that disciplina in a semester and  
get:

ActiveRecord::HasManyThroughCantAssociateThroughHasManyReflection:  
Cannot modify association ‘Disciplina#turmas’ because the source  
reflection class ‘Turma’ is associated to ‘DisciplinaPeriodo’  
via :has\_many.

So to create a Class for a DisciplineSemester entries which represents  
an Discipline offered in a Semester, I must do something like:

Class.create!(:code =\> ‘33A’, :start\_time =\> ‘13h’, :end\_time =\>  
‘15h’, :description =\> ‘Especial Class For  
Nerds’, :discipline\_semester\_id =\> PassHereTheID)

Sorry for the big message but I hope I made myself clear,

Thanks,
