Bad design or better way?


#1

I am at a loss as to whether this page of my site is poorly designed or
if
there is a better way to achieve what I am looking for.

My concern lays in one piece of controller code. I really don’t like
the
way I have to use params[:answers].values, nor do I like that for the
creation of each response it has to find the answer associated with it.
Any
help would be greatly appreciated. I’ve also put the basic models at
the
bottom of this email as well as the partial that creates the radio
buttons
for the form.

Thanks,
Michael G.

— Controller
def save
person = Person.new(params[:person])
breakpoint
params[:answers].values.each do |answer|
Response.create(:person => person, :answer =>
Answer.find(answer.to_i))
end
if person.save
flash[:notice] = ‘Test has been completed, Thanks!’
else
flash[:notice] = ‘Test could not be saved.’
end
end

— View
<%= radio_button_tag “answers[#{question.id}]”, answer.id, false, :id =>
answer.id %>

— Models
class Answer < ActiveRecord::Base
belongs_to :question
has_many :responses, :dependent => true
has_many :people, :through => :responses
end

class Person < ActiveRecord::Base
has_many :responses, :dependent => true
has_many :answers, :through => :responses
end

class Question < ActiveRecord::Base
belongs_to :survey
belongs_to :category
has_many :answers
end

class Response < ActiveRecord::Base
belongs_to :person
belongs_to :answer
end