0

I have three models: Lesson, Situation, Fate(join table). In this app, A situation can have many lessons and a lesson can belong to multiple situations.

I would essentially like the tables to look like this.

Situation

id.....name.....................description

1.....Ordering Food......You go into a restaurant and order food.

2.....Introduce yourself.You meet someone for the first time and you introduce yourself.

Lesson

id.....name............description..............lesson_text

1......Order food....How to order food..Blah blah blah, this is how you order food.

2......Call the waiter.How to call the waiter Blah blah blah, this is how you call the waiter

3 Pay for food How to pay for food Blah blah blah, this is how you pay for food.

4 Greet a person How to greet a person Blah blah blah, this is how you greet a person.

5 Ask a question How to ask a question Blah blah blah, this is how you ask a question.

Fate

situation_id lesson_id required

1.................1...............yes

1.................2...............yes

1.................3...............no

2.................3...............yes

2.................4...............yes

2.................5...............yes

I have the tables set up but I'm not sure how I would associate a lesson to a situation.

This is what my application looks like currently

Situations controller

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

Lessons controller

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

Routes

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

Thanks for the help! and I'm really sorry about the messy formatting.

Sunwoo Yang
  • 1,213
  • 2
  • 12
  • 22
  • what exactly is not working? Looks like you have has_many through association set up correctly. – Stpn Jun 24 '12 at 02:09
  • I'm not sure how to make the form that allows me to say a certain lesson belongs to a situation. I know how to create new lessons and situations but I'm not sure how I can associate them using Fate as my join table. I'm just generally confused because I've never made a has_many :through association before. Thanks for commenting! – Sunwoo Yang Jun 24 '12 at 02:10

1 Answers1

0

So if you are creating a new situation and want to also create new lessons that will be associated with it..

app/models/situation.rb

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

app/controllers/situations_controller.rb

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

  respond_to do |format|
   format.html 
  end
 end 

app/views/lessons/_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 %> 

Essentially you need a nested form (there is plenty examples). I typed this off the iPhone, hope it works )

Stpn
  • 6,202
  • 7
  • 47
  • 94