i have two models Vote and Option,each with column "total" and "quantity".
(Vote has_many:options)
I want to implement like this initially:
Option.quantities.each{ |quantity| total+=quantity}
Vote.total=total
how to implement that??
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Are you asking for an alternate implementation? Or for something else? You can do the same thing with
Enumerable::inject, for example:In this way, the Vote total is calculated and saved every time
calculate_totalis called.