5

我正在开发 Rails 应用程序,目前我有 2 个模型 - 主题和课程。一个主题有 3 种不同类型的课程 - 讲座、教程和实验室。我建模使得课程模型有 3 个 has_one。

现在,我正在尝试为主题和课程创建一个嵌套表单,但要保存的讲座、教程和实验室始终是第一个呈现的表单。即我有 3 个分别用于 Lecture、Tutorial 和 Laboratory 的嵌套表单,但保存的 Lecture、Tutorial 和 Laboratory 始终是第一个构建的。在我的代码中,首先构建了讲座,因此教程和实验室的属性将遵循我为讲座填写的属性。

我不确定我哪里出错了,或者即使在这种情况下有多个 has_one 关系有效,所以任何建议都将不胜感激。

相关代码如下:

主体模型

class Subject < ActiveRecord::Base

  has_one :lecture, :class_name => "Lesson"
  has_one :laboratory,:class_name => "Lesson"
  has_one :tutorial, :class_name => "Lesson"

  accepts_nested_attributes_for :lecture
  accepts_nested_attributes_for :laboratory
  accepts_nested_attributes_for :tutorial 

end

上课模式

class Lesson < ActiveRecord::Base
  belongs_to :subject
end

主题和课程嵌套形式

<%= form_for(@subject_list) do |f| %>
  <div class="field">
    <%= f.label :subject_code %><br />
    <%= f.text_field :subject_code %>
  </div>
  <div>
    <%= f.fields_for :lecture do |lecture| %>
      <%= render "lecture_fields", :f => lecture %>
    <% end %>
  </div>
  <div>
    <%= f.fields_for :tutorial do |tutorial| %>
      <%= render "tutorial_fields", :f => tutorial %>
    <% end %>
  </div>
  <div>
    <%= f.fields_for :laboratory do |laboratory| %>
      <%= render "laboratory_fields", :f => laboratory %>
    <% end %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

主题控制器中的新操作

    def new
    @subject = Subject.new

    lecture = @subject.build_lecture
    laboratory = @subject.build_laboratory
    tutorial = @subject.build_tutorial

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @subject }
    end
  end

如果有人可以帮助我确定我哪里出错了,我将不胜感激。如果在我不应该创建这样的多重关系的情况下,我想就如何实际渲染 3 个带有指示课程类型的默认字段的表单提供一些建议。

4

2 回答 2

7

我不确定这是否有效,但我的建议是使用 AR 继承

class Lesson < ActiveRecord::Base
end

class LectureLesson < Lesson
  belongs_to :subject
end

class LaboratyLesson < Lesson
  belongs_to :subject
end

class TutorialLesson < Lesson
  belongs_to :subject
end

class Subject
  has_one :lecture_lesson
  has_one :laboratory_lesson
  has_one :tutorial_lesson

  accepts_nested_attributes_for :lecture_lesson
  accepts_nested_attributes_for :laboratory_lesson
  accepts_nested_attributes_for :tutorial_lesson
end

移民

class LessonsAndSubjects < ActiveRecord::Migration
  def up
    remove_column :subjects, :lesson_id

    add_column :subjects, :lecture_lesson_id, :integer
    add_column :subjects, :laboratory_lesson_id, :integer
    add_column :subjects, :tutorial_lesson_id, :integer

    add_column :lessons, :type, :string

    add_index :subjects, :lecture_lesson_id
    add_index :subjects, :laboratory_lesson_id
    add_index :subjects, :tutorial_lesson_id
  end

  def down
    remove_column :subjects, :lecture_lesson_id
    remove_column :subjects, :laboratory_lesson_id
    remove_column :subjects, :tutorial_lesson_id

    remove_column :lessons, :type

    add_column :subjects, :lesson_id, :integer
  end
end

它更有意义,它可能会解决嵌套属性的问题

于 2013-01-19T15:38:00.193 回答
0

实际上,从 rorra 的答案来看,缺少一点,您需要为每个“孩子”添加一个多态关联,以免引发查询问题

class Lesson < ActiveRecord::Base
  belongs_to :subject
end

class LectureLesson < Lesson
  belongs_to :polymorphic_lecture_lesson, polymorphic: true
end

class Subject
  has_one :lesson
  has_one :lecture_lesson, as: :polymorphic_lecture_lesson

  accepts_nested_attributes_for :lesson
  accepts_nested_attributes_for :lecture_lesson
end

在迁移中,您必须添加

add_column :lessons, :polymorphic_lecture_lesson_id, :integer, index: true
add_column :lessons, :polymorphic_lecture_lesson_type, :integer, index: true
于 2014-10-06T21:23:40.703 回答