It's one of the test tasks I've done and published on GitHub so far.
After you cloned the project and run installation of gems you need to configure database.yml
The project already has database_default.yml
setup to Postgres database. Just put your postgres username (role) and password.
Having configured database.yml
you need put the following to terminal:
bundle exec rake db:create && bundle exec rake db:migrate && bundle exec rake db:seed
Which does all at once: creating databases, running migrations and seeding the database. If you wish you can do one at a time.
Then just run rails c
in terminal.
There are players. Players belong to teams. Teams play matches (games). Players may get some sort of achievements done (we'll call it results) while they play a match. Something like "run 10k" or "70% accuracy of a ball being successfully passed".
We need to create Rails models that describe it and enable some features to be used along.
- Record a result for a player to our database;
- Write a method that answers to a question - did a player achieve a specific result in the last 5 matches for a team? Just Yes or No.
- Get Top 5 players that achieved a specific result the most often, in one team.
- Get Top 5 players that achieved a specific result the most often, among all teams.
Now have a Rails app (console, to be exact), up and running with our new features onboard.
Here we use the create
action from our ResultController
.
So, for example, we have these simple routes:
get 'result', to: 'result#new'
post 'result', to: 'result#create'
That gives us the following paths:
new_result_path
create_result_path
Which we use to first initialize a result object to later save it.
In theory, it's possible to use controllers through routes in rails console. Though the last isn't recomended since reliability of avalailble ways to do so is not guaranteed.
2. Write a method that answers to a question - did a player achieve a specific result in the last 5 matches for a team? Just Yes or No.
That's where our rails console is useful.
To do so you need to call achieved_result_in_last_matches?
on an instance of Player - specifying result (string) and optionally quantity of matches:
Player.first.achieved_result_in_last_matches?('run_ten_km', 3) # second argument - matches_amount is optional, by default it's 5
We get a boolean value returned.
Here you use a class method top_by_result_one_team
- where you pass a team object, result (string) and optionally quantity of players:
PlayerResult.top_by_result_one_team(Team.first, 'run_ten_km', 6) # third argument - players_amount is optional, by default it's 5
We get players' objects returned.
Again, a class method, this time call top_by_result_all_teams
- where you pass result (string) and optionally quantity of players:
PlayerResult.top_by_result_all_teams('run_ten_km', 4) # second argument - players_amount is optional, by default it's 5
We get players' objects returned.