Forum: Ruby on Rails ActiveRecord::HasManyThroughCantAssociateThroughHasManyReflection

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fba7e4a85ab39970c6168db448b836ed?d=identicon&s=25 Celio Motta (Guest)
on 2009-04-14 17:33
(Received via mailing list)
Hello,

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')

ActiveRecord::HasManyThroughCantAssociateThroughHasManyReflection:
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
end

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
end

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
end

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