views:

47

answers:

4

I have a table with question_id , nominees and vote_count in which the values for question_id and nominees are populated from other tables with vote_count as zero.

ex:

question_id nominees vote_count
1 tamil 0
1 selvi 0

2 aaaa 0
3 qqqq 0
3 wwww 0

If the users 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,

<% 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

any help on this...

A: 

vote_count+=1;

Yuval A
laughing my ass off
b0x0rz
I'm tempted to give +1 (ie. vote_count++) for smartassing
balpha
Your flippant answer doesn't actually work in Ruby.
Pesto
A: 

Presumably, you have a table VOTES with fields voter_id and voted_for. For every new vote, INSERT the voter and that person's vote in that field. When you do a SELECT COUNT(*) FROM VOTES WHERE voted_for = candidate_id, you will get the updated vote count.

Sinan Ünür
A: 

If you have voting.update_attribute inside a loop in your view then it will be updating the vote count for each voting every time the page is requested.

What you probably want is an action in your controller which updates the attribute for a single voting when the request is submitted.

mikej
A: 

There is not enough information to go on here but it seems you have a many to many relationship, so a join table is probably the way to go. Depending on your implementation (it look like this is all being done in the view) you could use increment(vote) and counter_cache to do all the heavy lifting.

Please post your actual model, controller, and view code.

askegg