Eden B. wrote:
I have a controller method called update_all that grabs parameters for
about
30 form fields and saves them to the database. My current code looks to
see
if there is a string in params[] for each form field (two fields for
each
SelfEvaluationItem) and saves the updated self_evaluation_answer if a
string
is available. Since this is iterated code, I am almost certain that
this
generates 30 or so SQL queries.
Is there a way to build up all of the self_evaluation_answers into a
larger
unit (model array?) and then save them all at once? The goal of course
would be to reduce the number of queries to just 1. I guess the
alternate
would be to build a SQL query to do this and execute it, but I prefer to
work in Ruby and Rails if possible.
Any help is very much appreciated!
I have included the source for reference below:
def update_all
for item in SelfEvaluationItem.find_all
id = params[:self_evaluation_answer][item.id.to_s][:id]
comment = params[:self_evaluation_answer][item.id.to_s][:comment]
response =
params[:self_evaluation_answer][item.id.to_s][:response]
if !comment.empty? or !response.empty?
@self_evaluation_answer = SelfEvaluationAnswer.find(id)
@self_evaluation_answer.comment = comment unless comment.empty?
@self_evaluation_answer.response = response unless
response.empty?
@self_evaluation_answer.save
end
end
flash[:notice] = “Save Completed”
end
If I’m reading this correctly, what you’re doing is cycling through
every single item in your SelfEvaluationItem table to compare each item
against the entries in params[:self_evaluation_answer]. Wouldn’t it be
more efficient to cycle through params[:self_evaluation_answer] and
refer to evaluation items based on the results?
For example:
def update_all
params[:self_evaluation_answer].each do |answer|
answer.each_key do |id_from_answer|
item = SelfEvaluationAnswer.find(id_from_answer.to_i)
new_comment = answer[id_from_answer][:comment]
new_response = answer[id_from_answer][:response]
unless new_comment.empty? and new_response.empty?
@self_evaluation_answer = item
@self_evaluation_answer.comment = new_comment unless
new_comment.empty?
@self_evaluation_answer.response = new_response unless
new_response.empty?
@self_evaluation_answer.save
end
end
end
flash[:notice] = “Save Completed”
end
Again I’m hoping I have your read your code correctly, but what I
attemped to do here is to loop through the
params[:self_evaluation_answer] hash, which seems to have keys that are
String versions of your SelfEvaluationItem primary key ids.
So we take each key and use it to find the SelfEvaluationItemAnswer
record which matches that id. Then if there’s a new comment or response
we add them to the record, and save it.
I added descriptive flash messages indicating when an error prevented
the record from saving or when the record didn’t need to be updated.
I’m not sure this really addresses the gist of your question, but I
thought I’d offer it as an alternative that might be slightly more
efficient. Feel free to disregard if this doesn’t suit your needs for
whatever reason.
Jeff C.man