No se pudo encontrar el problema de asociación en Rails


Soy bastante nuevo en Ruby on Rails, y claramente tengo un problema de asociación de registros activos, pero no puedo resolverlo por mi cuenta.

Dadas las tres clases modelo con sus asociaciones:

# application_form.rb
class ApplicationForm < ActiveRecord::Base
  has_many :questions, :through => :form_questions
end

# question.rb
class Question < ActiveRecord::Base
  belongs_to :section
  has_many :application_forms, :through => :form_questions
end

# form_question.rb
class FormQuestion < ActiveRecord::Base
  belongs_to :question
  belongs_to :application_form
  belongs_to :question_type
  has_many :answers, :through => :form_question_answers
end

Pero cuando ejecutoel controlador para agregar preguntas a los formularios de solicitud, obtengo el error:

ActiveRecord::HasManyThroughAssociationNotFoundError in Application_forms#show

Showing app/views/application_forms/show.html.erb where line #9 raised:

Could not find the association :form_questions in model ApplicationForm

Puede alguien señalar lo que estoy haciendo mal?

Author: Ash, 2009-11-23

2 answers

En la clase ApplicationForm, debe especificar la relación de ApplicationForms con 'form_questions'. Aún no lo sabe. En cualquier lugar que use :through, primero debe indicarle dónde encontrar ese registro. El mismo problema con tus otras clases.

So

# application_form.rb
class ApplicationForm < ActiveRecord::Base
  has_many :form_questions
  has_many :questions, :through => :form_questions
end

# question.rb
class Question < ActiveRecord::Base
  belongs_to :section
  has_many :form_questions
  has_many :application_forms, :through => :form_questions
end

# form_question.rb
class FormQuestion < ActiveRecord::Base
  belongs_to :question
  belongs_to :application_form
  belongs_to :question_type
  has_many :form_questions_answers
  has_many :answers, :through => :form_question_answers
end

Eso es asumiendo que así es como lo tienes configurado.

 61
Author: Jim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-11-23 05:27:33

Debe incluir un

has_many :form_question_answers

En su modelo FormQuestion. El: through espera una tabla que ya ha sido declarada en el modelo.

Lo mismo ocurre con sus otros modelos't no puede proporcionar una asociación has_many :through hasta que haya declarado primero el has_many

# application_form.rb
class ApplicationForm < ActiveRecord::Base
  has_many :form_questions
  has_many :questions, :through => :form_questions
end

# question.rb
class Question < ActiveRecord::Base
  belongs_to :section
  has_many :form_questions
  has_many :application_forms, :through => :form_questions
end

# form_question.rb
class FormQuestion < ActiveRecord::Base
  belongs_to :question
  belongs_to :application_form
  belongs_to :question_type
  has_many :form_question_answers
  has_many :answers, :through => :form_question_answers
end

Parece que su esquema puede ser un poco torcido, pero el punto es que siempre necesita agregar el has_many para la tabla de unión primero, luego agregar el through.

 12
Author: bensie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-11-23 05:21:31