Rails与同一模型的多个Has_one关系

时间:2013-01-19 13:36:52

标签: ruby-on-rails ruby ruby-on-rails-3 has-one rails-activerecord

我正在开发一个Rails应用程序,目前我有两个模型 - 主题和课程。 学科有3种不同类型的课程 - 讲座,教程和实验室。我模拟了Lesson模型中有3个has_one。

现在,我正在尝试为主题和课程创建嵌套表单,但是保存的讲座,教程和实验室始终是第一个呈现的表单。 即我分别为讲座,教程和实验室提供了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个表单提供一些建议。

2 个答案:

答案 0 :(得分:5)

我不确定这是否有效,但我的建议是使用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

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

答案 1 :(得分: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