Thursday, May 6, 2010

“Always two there are, a master and an apprentice.”


I'm having a blast hacking away on my project. Rails is a fun language to learn. Lately I've been hitting a wall on the execution side. I will be working and get to a point where everything I do isn't working. I know what I want to implement and have an idea of how it should be implemented but nothing seems to work. I run searches to find an example of what to do but come up empty. Things get dark when I am unable to figure it out.


Here's my dilemma, I have a form for a user to enter stats. The user enters 2 point field goals made/attempted, 3 point field goals made/attempted, free throws made/attempted, offensive/defensive rebounds, and others. I have other model attributes that the user doesn't provide input for on the form. They are total points, field goal %, 3 point field goal %, free throw %, and total rebounds. I'm thinking the model can take the attributes the user entered, obtain certain values and perform the calculations to populate the extra attributes all before the record is saved.

For example, to calculate the points scored for the current stat record being created;

# before record saves, do total_points method

before_save :total_points


# calculates total points of current record

def points

@twopoints = :twopfgm * 2

@threepoints = :threepfgm * 3

@onepoints = :ftm * 1

@twopoints + @threepoints + @onepoints

end


private

# update :total_points attribute of record with points

def total_points

stat.update_attribute(:total_points, points)

end



This doesn't work. I'm in the process of figuring this out.

No comments: