# Updating attribute based on radio selection

**URL:** <https://www.ruby-forum.com/t/updating-attribute-based-on-radio-selection/173117>\
**Category:** Rails\
**Created:** [August 3, 2009, 12:01pm UTC](https://www.ruby-forum.com/t/updating-attribute-based-on-radio-selection/173117 "2009-08-03T12:01:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/958977/32.png) [@sts](https://www.ruby-forum.com/u/sts)\
**Post date:** [August 3, 2009, 12:01pm UTC](https://www.ruby-forum.com/t/updating-attribute-based-on-radio-selection/173117/1 "2009-08-03T12:01:12Z")

</div>

I have a table with question\_id , nominees and vote\_count in which the  
values are added manually as follows.  
ex:

question\_id nominees vote\_count  
1 tamil 0  
1 selvi 0  
2 aaaa 0  
3 qqqq 0  
3 wwww 0

using this i have created a surevy.

when i select a nominee, then the vote count for the particular nominee  
should be updated like this.  
question\_id nominees vote\_count  
1 tamil 1  
1 selvi 0  
2 aaaa 1  
3 qqqq 0  
3 wwww 1

am implementing this as follow,  
\<% count = 1 %\>  
\<% for voting in @voting %\>  
\<% if voting.question\_id.eql?(count) %\>  
\<%= radio\_button( count, voting.vote\_count, :id =\> voting.nominees ) %\>  
\<%= voting.nominees %\>

\<% voting.update\_attribute(‘vote\_count’, voting.vote\_count+1 ) %\>  
\<% end %\>

but it updates vote\_count for all the nominees like

question\_id nominees vote\_count  
1 tamil 1  
1 selvi 1  
2 aaaa 1  
3 qqqq 1  
3 wwww 1

how can i compare the selected nominees with the existing one.

any help on this would be very useful…

---

<div class="post-metadata">

**Author:** ![sts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/958977/32.png) [@sts](https://www.ruby-forum.com/u/sts)\
**Post date:** [August 3, 2009, 1:41pm UTC](https://www.ruby-forum.com/t/updating-attribute-based-on-radio-selection/173117/2 "2009-08-03T13:41:39Z")

</div>

Why are you doing model updating in the view? Can you post the  
corresponding controller code?

You use a loop to read every record in your table, and inside that  
loop, the line:

voting.update\_attribute(‘vote\_count’, voting.vote\_count+1 )

updates every record adding one to the previous value. Your “if  
voting.question…” encloses only the line below it (It appears that  
\<% end %\> is missing.

On 3 ago, 11:01, Tamilselvi Srinivasan \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![sts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/958977/32.png) [@sts](https://www.ruby-forum.com/u/sts)\
**Post date:** [August 3, 2009, 1:58pm UTC](https://www.ruby-forum.com/t/updating-attribute-based-on-radio-selection/173117/3 "2009-08-03T13:58:09Z")

</div>

sorry,i have missed to paste the \<%end%\> in the post.

actually i need to compare the selected nominee with the available  
nominees .  
if both are equal then i need to update the vote\_count. so only i  
updated the model in view…

Eddy J. wrote:

> Why are you doing model updating in the view? Can you post the  
> corresponding controller code?
> 
> You use a loop to read every record in your table, and inside that  
> loop, the line:
> 
> voting.update\_attribute(‘vote\_count’, voting.vote\_count+1 )
> 
> updates every record adding one to the previous value. Your “if  
> voting.question…” encloses only the line below it (It appears that  
> \<% end %\> is missing.
> 
> On 3 ago, 11:01, Tamilselvi Srinivasan \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![sts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/958977/32.png) [@sts](https://www.ruby-forum.com/u/sts)\
**Post date:** [August 3, 2009, 2:32pm UTC](https://www.ruby-forum.com/t/updating-attribute-based-on-radio-selection/173117/4 "2009-08-03T14:32:00Z")

</div>

this post will be useful in understanding the logic you need for this  
task. stars, css and the works is upto your own discretion…

[http://blog.aisleten.com/2007/05/03/ajax-css-star-rating-with-acts\_as\_rateable/](http://blog.aisleten.com/2007/05/03/ajax-css-star-rating-with-acts_as_rateable/)

On Aug 3, 4:58 pm, Tamilselvi Srinivasan \<rails-mailing-l…@andreas-
