Monday, May 10, 2010

Check - It Works!

I figured it out. I have attributes in my Stat database table that the user doesn't provide input for. The value, for the hidden attributes, is gotten from the other attributes that the user does provide input for.

What I did:

In the Model:

before_validation :cache_virtual_columns
def total_points
((twopfgm * 2)+(threepfgm * 3)+(ftm * 1))
end
def field_goal_percent
sprintf("%0.3f", ((twopfgm+threepfgm)/(twopfga+threepfga.to_f)))
end

private
def cache_virtual_columns
self.total_points = total_points
self.field_goal_percent = field_goal_percent
end

In the Form:

%= f.hidden_field :total_points
%= f.hidden_field :field_goal_percent


When a new Stat record is created, it checks the Model and before doing validations does the cache_virtual_columns method. cache_virtual_columns has the total_points and field_goal_percent DB columns equal the value in their method. This updates the Stat record to contain values for each attribute.

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.

Sunday, May 2, 2010

To Boldly Go Where No Man(Me) Has Gone Before

Captain's log, Stardate 542010.5.

I've been working on my project for week's. The past couple days have been uneventful but I've made excellent progress. I am figuring out the relationships between League, Game, Player, and Stat. I've been learning about nesting resources and nesting models in one form. I've split them into pairs.

League has many Games. Game belongs to League.
Player has many Stats. Stat belongs to Player.

This gives me these routes.

/leagues
/leagues/:id
/leagues/:league_id/games
/leagues/:league_id/games/:id
/players
/players/:id
/players/:player_id/stats
/players/:player_id/stats/:id

Now I am able to create a league with many games and players with many stats. I'm working on getting the players with their stats to belong to a specific game in a specific league. I want to access players on its own so I'm hesitant to nest it. Plus, Rule of thumb: resources should never be nested more than 1 level deep. So I shouldn't do Games to Players to Stats.

I would like to have the form below to create the box score for each game. It will also track each players stats.
It would start with a drop-down menu to choose league. Selecting a league will activate the drop-down menu of games in that league. Selecting a game will do something that will allow me to get the form to choose a player from a drop-down menu and add their stats for that game. And do this for multiple players.

When I view the game I see


May the force be with you.