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
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-- Posted via http://www.ruby-forum.com/.
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
--
Posted via http://www.ruby-forum.com/.