Forum: Ruby on Rails Bad design or better way?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D337e2cf7c6d03caac6988d83e0e7f66?d=identicon&s=25 Michael Guterl (Guest)
on 2006-06-08 17:22
(Received via mailing list)
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 Guterl

--- 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
This topic is locked and can not be replied to.