如何使用连接表将数据相互关联?

时间:2012-06-24 00:51:32

标签: ruby-on-rails has-many-through

我有三个模型:课程,情境,命运(联接表)。 在这个应用程序中,情境可以有很多课程,课程可以属于多种情况。

我基本上希望这些表看起来像这样。

情况

ID .....名称.....................描述

1 .....订购食物......你去餐馆点菜。

2 .....介绍你自己。你第一次见到某人并自我介绍。

ID .....名............描述.............. lesson_text

1 ......订购食物....如何订购食物......等等等等,这就是你订购食物的方式。

2 ......打电话给服务员。如何打电话给服务员Blah等等等等,这就是你打电话给服务员的方式

3支付食物如何支付食物Blah等等等等,这就是你支付食物的方式。

4问候一个人如何问候一个人Blah等等等等,这就是你如何问候一个人。

5提出问题如何提出问题Blah blah blah blah,这就是你提问的方式。

命运

situation_id lesson_id required

1 ................. 1 ...............是

1 ................. 2 ...............是

1 ................. 3 ...............没有

2 ................. 3 ...............是

2 ................. 4 ...............是

2 ................. 5 ...............是

我已经设置了表格,但我不确定如何将课程与情况联系起来。

这是我的应用程序目前的样子

情境控制器

class SituationsController < ApplicationController

  def index
    @situations = Situation.all
  end

  def new
    @situation = Situation.new
  end

  def create
    @situation = Situation.new(params[:situation])
    respond_to do |format|
      if @situation.save
        format.html { redirect_to @situation }
      end
    end
  end

  def show
    @situation = Situation.find(params[:id])
    @lesson = @situation.lessons.new
  end

  def edit
    @situation = Situation.find(params[:id])
  end

  def update
    @situation = Situation.find(params[:id])
    respond_to do |format|
      if @situation.update_attributes(params[:situation])
        format.html { redirect_to @situation }
      end
    end
  end

  def destroy
    @situation = Situation.find(params[:id])
    @situation.destroy
    respond_to do |format|
      format.html { redirect_to situations_path }
    end
  end
end

课程控制器

class LessonsController < ApplicationController
  def index
    @lessons = Lesson.all
  end

  def new
    @lesson = Lesson.new
  end

  def create
    @lesson = Lesson.new(params[:lesson])
    respond_to do |format|
      if @lesson.save
        format.html { redirect_to @lesson }
      end
    end
  end

  def show
    @lesson = Lesson.find(params[:id])
  end

  def edit
    @lesson = Lesson.find(params[:id])
  end

  def update
    @lesson = Lesson.find(params[:id])
    respond_to do |format|
      if @lesson.update_attributes(params[:lesson])
        format.html { redirect_to @lesson }
      end
    end
  end

  def destroy
    @lesson = Lesson.find(params[:id])
    @lesson.destroy
    respond_to do |format|
      format.html { redirect_to lessons_path }
    end
  end
end

路线

root :to => 'situations#index'

  resources :situations do
    resources :lessons
  end
  resources :difficulties

Situation.rb

class Situation < ActiveRecord::Base
  attr_accessible :name, :description

  has_many :fates
  has_many :lessons, :through => :fates
end

Lesson.rb

class Lesson < ActiveRecord::Base
  attr_accessible :name, :description, :difficulty_id, :lesson_text

  has_many :fates
  has_many :situations, :through => :fates
end

Fate.rb

class Fate < ActiveRecord::Base

  belongs_to :lesson
  belongs_to :situation
end

感谢您的帮助!对于凌乱的格式化,我真的很抱歉。

1 个答案:

答案 0 :(得分:0)

因此,如果您正在创建新情况并希望创建与其相关联的新课程..

应用程序/模型/ situation.rb

attr_accessible :name, :description, :difficulty_id, :lesson_text, :lessons_attributes             
accepts_nested_attributes_for :lessons

应用程序/控制器/ situations_controller.rb

def new
 @situation = Situation.new
 2.times do{@situation.lessons.build}

  respond_to do |format|
   format.html 
  end
 end 

应用程序/视图/经验/ _form.html.erb

<% form_for @situation do |f| %>

  <%= f.text_field :name %>
  <%= f.text_field :description %>


  <%  f.fields_for :situations do |lesson_field| %>  
   <%= lesson_field.text_field :name %>
   <%= lesson_field.text_field :lesson_text %>
  <%  end %>

 <%= f.submit %>
<% end %> 

基本上你需要一个嵌套的表单(有很多例子)。我在iPhone上输入了这个,希望它有效)

相关问题