Ian Kennedy
2005-Oct-18 16:17 UTC
Re: inserting multiple records into a table (hack solution)
i wound up getting the hash from params and inserting them one by one
in a really hackish way. anybody have anything better? a more
ruby/railsy way of doing this?
############
my create def:
############
def create
@course = Course.new(params[:course])
@question = Question.new(params[:question])
@answers = @params[:answer]
if @course.save
flash[:notice] = ''Course was successfully created.''
flash[:course_id] = @course.id
flash[:answers] = @answers
@question.course_id = @course.id
@question.save
answers = Answer.create(
[ { :course_id => @course.id,
:question_id => @question.id,
:answer => @answers["answer1"],
:correct => @answers["correct1"],
:position => "1"
},
{ :course_id => @course.id,
:question_id => @question.id,
:answer => @answers["answer2"],
:correct => @answers["correct2"],
:position => "2"
},
{ :course_id => @course.id,
:question_id => @question.id,
:answer => @answers["answer3"],
:correct => @answers["correct3"],
:position => "3"
},
{ :course_id => @course.id,
:question_id => @question.id,
:answer => @answers["answer4"],
:correct => @answers["correct4"],
:position => "4"
}
]
)
redirect_to (:controller => ''questions'', :action =>
''new'')
else
render :action => ''new''
end
end
#############
my _form partial
#############
<% 1.upto(4) do |i| %>
<tr>
<td>
<b><label for="answer_answer">Answer <%= i.to_s
%></label></b><br/>
<%= text_field ''answer'', ''answer'' +
i.to_s %>
</td>
<td>
<b><label for="answer_correct">Correct
answer?</label></b><br/>
<%= check_box ''answer'', ''correct'' +
i.to_s %>
</td>
</tr>
<% end %>
##############
what i''d like to do
##############
answers.each{|answer| Answer.create answer}
thanks, ian
--
--
Ian Kennedy
http://www.fiftymillimeter.com
Tom Davies
2005-Oct-18 17:23 UTC
Re: Re: inserting multiple records into a table (hack solution)
Hi Ian,
I am not sure I follow exactly what you are doing, but one thing that
might make things easier is to store all of the attributes you want to
save in separate objects. For example,
<% 1.upto(4) do |i| %>
<tr>
<td>
<%= text_field "answer#{i}", ''answer'' %>
</td>
<td>
<%= check_box "answer#{i}", ''correct'' %>
</td>
</tr>
<% end %>
Then you could just grab each answer and create a new one using:
(1..4).each do |i|
Answer.new(@params["answer#{i}"])
end
Also, I am not sure how you setup your relationships, but it seems
that a Course has_many Questions and a Question has_many Answers.
In that case you could just say:
@course = Course.new(@params[:course])
@question = Question.new(@params[:question])
@course.questions << @question
And then the loop for answers would be more like:
(1..4).each do |i|
@question.answers.build(@params["answer#{i}"])
end
All of the above is untested... but hopefully what I said makes sense.
Good luck,
Tom
On 10/18/05, Ian Kennedy
<exposure-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> i wound up getting the hash from params and inserting them one by one
> in a really hackish way. anybody have anything better? a more
> ruby/railsy way of doing this?
>
> ############
> my create def:
> ############
>
> def create
> @course = Course.new(params[:course])
> @question = Question.new(params[:question])
> @answers = @params[:answer]
> if @course.save
> flash[:notice] = ''Course was successfully created.''
> flash[:course_id] = @course.id
> flash[:answers] = @answers
> @question.course_id = @course.id
> @question.save
> answers = Answer.create(
> [ { :course_id => @course.id,
> :question_id => @question.id,
> :answer => @answers["answer1"],
> :correct => @answers["correct1"],
> :position => "1"
> },
> { :course_id => @course.id,
> :question_id => @question.id,
> :answer => @answers["answer2"],
> :correct => @answers["correct2"],
> :position => "2"
> },
> { :course_id => @course.id,
> :question_id => @question.id,
> :answer => @answers["answer3"],
> :correct => @answers["correct3"],
> :position => "3"
> },
> { :course_id => @course.id,
> :question_id => @question.id,
> :answer => @answers["answer4"],
> :correct => @answers["correct4"],
> :position => "4"
> }
> ]
> )
> redirect_to (:controller => ''questions'', :action
=> ''new'')
> else
> render :action => ''new''
> end
> end
>
> #############
> my _form partial
> #############
>
> <% 1.upto(4) do |i| %>
> <tr>
> <td>
> <b><label for="answer_answer">Answer <%= i.to_s
%></label></b><br/>
> <%= text_field ''answer'', ''answer''
+ i.to_s %>
> </td>
> <td>
> <b><label for="answer_correct">Correct
answer?</label></b><br/>
> <%= check_box ''answer'', ''correct''
+ i.to_s %>
> </td>
> </tr>
> <% end %>
>
> ##############
> what i''d like to do
> ##############
> answers.each{|answer| Answer.create answer}
>
>
> thanks, ian
>
> --
> --
> Ian Kennedy
> http://www.fiftymillimeter.com
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>