Alok Mysore
Alok Mysore

Reputation: 616

Can't save mongoid hash value

I have a mongoid object

@tran = Translations.where({:_id => params[:id]})[0]

The object @tran has a array of hashes at @tran[:translations]

I tried changing the value of a hash in the array like so:

@tran[:translations][0]['rated'] = (@tran[:translations][0]['rated']+1)

and I did a @tran.save

But the value does not seem to be updated.

What am I doing wrong here?

PS, Here's the value of @tran[:translations] : [{"value":"hello3","rating":100,"rated":0}]

Upvotes: 1

Views: 316

Answers (1)

Emu
Emu

Reputation: 5905

@tran = Translation.find params[:id]

You can use this line ->

@tran.update_attributes(:rated => @tran.rated+1)

Or this line ->

@tran.rated += 1
@tran.save

Upvotes: 1

Related Questions