ActiveRecord - Data Modeling


#1

Hello!

I am coming from 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 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,