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.