Ive got 3 models. Question, Answer and Result.
Got a middle model answers_results with an answer_id and a result_id.
The questions and answers are already in the database, so when someone starts a new survey it needs to save a result_id and answer_id in the answers_results table.
This is how my html looks like:
<input id="answer_id_5" name="answer_id[5]" type="checkbox" value="5">
This is how my answer model looks like:
class Answer < ActiveRecord::Base
attr_accessible :content, :question_id, :value
belongs_to :question
has_and_belongs_to_many :results, :join_table => :answers_results
end
and here is my Result model:
class Result < ActiveRecord::Base
attr_accessible :animal_id
has_and_belongs_to_many :answers, :join_table => :answers_results
end
Result controller:
class ResultsController < ApplicationController
def new
@result = Result.new
@animal = Animal.find(params[:id])
end
def create
@result = Result.new(params[:result])
if @result.save
redirect_to @result
else
render 'new'
end
end
def show
@result = Result.find(params[:id])
end
end
But it doesnt save the answer_id to the answers_results. It does save the result_id.
So my question is: How do I save the answer_id to the answers_results table.
Thanks,,