5

私はRailsアプリケーションに取り組んでおり、現在、サブジェクトとレッスンの2つのモデルがあります。科目には、講義、チュートリアル、実験の 3 種類のレッスンがあります。Lessonモデルにhas_oneが3つあるようにモデル化しました。

現在、科目とレッスン用にネストされたフォームを作成しようとしていますが、保存されているレクチャー、チュートリアル、ラボは常にレンダリングされた最初のフォームでした。つまり、レクチャー、チュートリアル、ラボ用に別々に 3 つのネストされたフォームがありますが、保存されたレクチャー、チュートリアル、ラボは常に最初に作成されたものでした。私のコードでは、講義が最初に作成されたため、チュートリアルと実験室の属性は、講義用に入力した属性に従います。

どこが間違っていたのか、この場合に複数の 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 からの回答から 1 つのポイントが欠落しています。クエリの問題が発生しないように、「子」ごとにポリモーフィックな関連付けを追加する必要があります。

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 に答える