Forum: Ruby on Rails ActiveRecord::HasManyThroughCantAssociateThroughHasManyReflection

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Celio Motta (Guest)
on 2009-04-14 19:33
(Received via mailing list)

I tried the following and got that error and can't figure out how to
fix it... I know I can do a long way to add Turmas To a Disciplina but
using like the following would be very convinient:

p = Periodo.find(:first)
d = p.disciplinas.find(13)
d.turmas.create!(:codigo => '23A', :horario => '2a e 4a -
15-17h', :descricao => 'Turma Regular')

Cannot modify association 'Disciplina#turmas' because the source
reflection class 'Turma' is associated to 'DisciplinaPeriodo'
via :has_many.

My models are:

class Periodo < ActiveRecord::Base
  has_many :disciplina_periodos, :dependent => :destroy
  has_many :disciplinas, :through => :disciplina_periodos

  validates_presence_of :periodo
  validates_uniqueness_of :periodo

class Disciplina < ActiveRecord::Base
  has_one :disciplina_periodo
  has_one :periodo, :through => :disciplina_periodo

  has_many :disciplina_periodos, :dependent => :destroy
  has_many :turmas, :through => :disciplina_periodos

  validates_presence_of :codigo, :nome
  validates_uniqueness_of :codigo
  validates_uniqueness_of :nome

class DisciplinaPeriodo < ActiveRecord::Base
    has_many :turmas, :dependent => :destroy
  belongs_to :periodo
  belongs_to :disciplina

  validates_presence_of :periodo_id, :disciplina_id
  validates_uniqueness_of :disciplina_id, :scope => :periodo_id
  validates_uniqueness_of :periodo_id, :scope => :disciplina_id

class Turma < ActiveRecord::Base
        belongs_to :disciplina_periodo
  belongs_to :disciplina
  validates_uniqueness_of :codigo, :scope => :disciplina_periodo_id
This topic is locked and can not be replied to.