Forum: Ruby on Rails How to update specific fields?

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.
cool_screen_name90001 (Guest)
on 2005-11-12 13:49
(Received via mailing list)
I can't figure this out. I just want to update two
fields in the database:

def vote
  # yes, I need to "to_i" these values!
  @id = @params["id"]
  @vote = @params["vote"]

  # doesn't work - not a class method
  #Item.update_attributes {["vote_count=vote_count+1,
vote_total=vote_total + ?", @vote]}

  # how to update vote_total by X?
  #Item.increment_counter "vote_count", @id
  #Item.increment_counter "vote_total", @id

  # error: undefined method `stringify_keys!' for
["vote_count=vote_count+1, vote_total=vote_total + ?",
"5"]:Array
  #Item.update @id, ["vote_count=vote_count+1,
vote_total=vote_total + ?", @vote]

  # error: undefined method `stringify_keys!' for
"vote_count=vote_count+1, vote_total=vote_total +
5":String
  #Item.update @id, "vote_count=vote_count+1,
vote_total=vote_total + #{@vote}"

  # error: undefined method `+' for :vote_count:Symbol
  Item.update @id, {:vote_count => :vote_count + 1,
:vote_total => :vote_total + @vote }

  flash[:notice] = "vote saved!"

  redirect_to :action=>'item', :id=>@params["id"]
end

BTW, does the "update" method first retrive the item,
update it, then retrive it again? If so, definitely
doesn't seem efficient if all you want to do is update
something.

thanks,
csn




__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005
http://mail.yahoo.com
This topic is locked and can not be replied to.