# Spot the error (I can't, I'm new)

**URL:** <https://www.ruby-forum.com/t/spot-the-error-i-cant-im-new/57798>\
**Category:** Rails\
**Created:** [April 14, 2006, 8:22am UTC](https://www.ruby-forum.com/t/spot-the-error-i-cant-im-new/57798 "2006-04-14T08:22:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![B\_Gates](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@B\_Gates](https://www.ruby-forum.com/u/B_Gates)\
**Post date:** [April 14, 2006, 8:22am UTC](https://www.ruby-forum.com/t/spot-the-error-i-cant-im-new/57798/1 "2006-04-14T08:22:16Z")

</div>

I have a form that I want to use to update multiple  
objects. In the controller,  
@grades = Grade.find(params[:grade].keys)  
@grades.each\_with\_index do |grade, i|  
grade.update\_attribute(params[:grade][i])  
end  
all\_valid = @grades.inject(true) {|memo, c|  
c.valid? && memo }  
this doesn’t update the attributes as I would expect.  
(I would just use  
Grade.update(params[:grade].keys,params[:grade].values)  
but I can’t figure out how to validate the data using  
that method. Could someone please explain what I’m  
doing wrong here?

Thanks,  
Brian Gates

* * *

Do You Yahoo!?  
Tired of spam? Yahoo! Mail has the best spam protection around

> **[Yahoo Mail - Organized Email](https://mail.yahoo.com)**
>
> It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...

---

<div class="post-metadata">

**Author:** ![B\_Gates](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@B\_Gates](https://www.ruby-forum.com/u/B_Gates)\
**Post date:** [April 14, 2006, 2:53pm UTC](https://www.ruby-forum.com/t/spot-the-error-i-cant-im-new/57798/2 "2006-04-14T14:53:35Z")

</div>

For one thing, remember that update\_attribute is for updating a single  
attribute. It looks like you’re updating multiple attributes per grade  
in your each loop, so you probably want to use update\_attributes  
instead, no?

B Gates wrote:

> I have a form that I want to use to update multiple  
> objects. In the controller,  
> @grades = Grade.find(params[:grade].keys)  
> @grades.each\_with\_index do |grade, i|  
> grade.update\_attribute(params[:grade][i])  
> end  
> all\_valid = @grades.inject(true) {|memo, c|  
> c.valid? && memo }  
> this doesn’t update the attributes as I would expect.  
> (I would just use  
> Grade.update(params[:grade].keys,params[:grade].values)  
> but I can’t figure out how to validate the data using  
> that method. Could someone please explain what I’m  
> doing wrong here?
> 
> Thanks,  
> Brian Gates
> 
> * * *
> 
> Do You Yahoo!?  
> Tired of spam? Yahoo! Mail has the best spam protection around  
> [http://mail.yahoo.com](http://mail.yahoo.com)

---

<div class="post-metadata">

**Author:** ![B\_Gates](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@B\_Gates](https://www.ruby-forum.com/u/B_Gates)\
**Post date:** [April 14, 2006, 6:07pm UTC](https://www.ruby-forum.com/t/spot-the-error-i-cant-im-new/57798/3 "2006-04-14T18:07:27Z")

</div>

update attribute takes 2 parameters: the name of the attribute and its  
new value, you are only giving it the value, and it doesnt know what  
field you are talking about.

Try:  
grade.update\_attribute(:letter\_grade, params[:grade][i])

Or with a bit of refactoring:  
@grades = []  
params[:grade].each\_pair do |key, value|  
grade = Grade.find(key)  
grade.update\_attribute(:letter\_grade, value)  
@grades \<\< grade  
end  
all\_valid = @grades.inject(true) do |memo, c|  
c.valid? && memo  
}

This wau you don;t have to use “params[:grade][i]” which is standard  
practice in other languages, but in ruby it’s usually quite unnesesary.

-Alex

B Gates wrote:

> I have a form that I want to use to update multiple  
> objects. In the controller,  
> @grades = Grade.find(params[:grade].keys)  
> @grades.each\_with\_index do |grade, i|  
> grade.update\_attribute(params[:grade][i])  
> end  
> all\_valid = @grades.inject(true) {|memo, c|  
> c.valid? && memo }  
> this doesn’t update the attributes as I would expect.  
> (I would just use  
> Grade.update(params[:grade].keys,params[:grade].values)  
> but I can’t figure out how to validate the data using  
> that method. Could someone please explain what I’m  
> doing wrong here?
> 
> Thanks,  
> Brian Gates
> 
> * * *
> 
> Do You Yahoo!?  
> Tired of spam? Yahoo! Mail has the best spam protection around  
> [http://mail.yahoo.com](http://mail.yahoo.com)
