Activerecord Update syntax


#1

This what I have - sadly, it does not work :frowning: .

posts = {params[:topic][:forum_id] => { “posts_count” => “posts_count +
@topic.posts_count”}, @topic.forum_id => { “posts_count” => "posts_count

  • @topic.posts_count" } }

Forum.update(posts.keys, posts.values)

It’s suppose to subtract the posts_count from topic, and add it and
subtract it from posts_count from each board, respectively.

I am fairly sure that “posts_count - @topic.posts_count” is what’s
stopping it from working and was wondering how would i go about this
better? Or if anyone can suggest an entirely better way to do this
transaction that would also be appreciated.

Thanks in advance!


#2

Anyone have an ideas?


#3

eddy wrote:

Anyone have an ideas?

First of all, your setting the values in the hash to a string. No
actual caclulations will happen here. You probably want something more
like this that uses update_attributes which accepts a hash of values.

topic = Topic.find(params[:id])
topic.post_count = #some calulation here.
topic.update_attributes(params[:topic])