Forum: Ruby on Rails spot the error (I can't, I'm new)

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.
B Gates (Guest)
on 2006-04-14 10:22
(Received via mailing list)
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
Steve K. (Guest)
on 2006-04-14 16:53
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
Alex W. (Guest)
on 2006-04-14 20:07
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
This topic is locked and can not be replied to.