Hello, what would be the best way to solve this? I have a view where I want users to be able to enter the score of a tennis match. The number of sets to be played is determined from a column in a database table. Each sports club, when set up, can set the total number of sets to be played in a match...5, 3, 1...whatever. This number will determine how many text boxes are displayed for the user to enter the score. The match model has only one column for score, so before the score is saved in the database it must be created as follows final_score = set_1 + set_2 + set_3 (for a match in which 3 sets must be played) I think maybe using virtual attributes is the way to go, but I am not sure how to implement it. Thanks. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
if you name each of your (game)set input fields something like "set[]" then rails will parse the parameters as an array. This way you will give you a params hash like: { ''match'' => { ''sets'' => [''1'',''3'',''7'']} } This means in your model you can define a virtual attribute called ''sets'' like: def sets= scores self.score = scores.map(&:to_i).sum end On Feb 18, 3:44 pm, Maulin pa <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello, > what would be the best way to solve this? > > I have a view where I want users to be able to enter the score of a > tennis match. The number of sets to be played is determined from a > column in a database table. Each sports club, when set up, can set the > total number of sets to be played in a match...5, 3, 1...whatever. > > This number will determine how many text boxes are displayed for the > user to enter the score. The match model has only one column for score, > so before the score is saved in the database it must be created as > follows > > final_score = set_1 + set_2 + set_3 (for a match in which 3 sets must be > played) > > I think maybe using virtual attributes is the way to go, but I am not > sure how to implement it. > > Thanks. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> def sets= scores > self.score = scores.map(&:to_i).sum > end >Thanks! I will try that. Also, would it be possible to add validations in the model for each of the sets? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Thanks! I will try that. Also, would it be possible to add validations > in the model for each of the sets?I don''t think you can use rails'' standard ''valtidates'' methods, like validates_presence_of, but you can add your own custom validate method to your model: def validate self.errors.add(:sets, ''are invalid'') unless <your validation code here> end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---