Algorithm implementation into Rails - Best Practices

I got a sweet independent contract through a friend for a Fantasy Golf player optimizer. I feel I may be a little over my head because my previous experience in my year and a half of Rails was simple CRUD apps. This is a little more complicated and I was wondering how a professional Rails shop like Thoughtbot would approach this with best practices. My client is a big golf statistics nut and saw this line optimizer for football:

He is trying to create one for golf. He worked out a fairly simple excel formula for calculating the top 6 players from a table of hundreds of players depending on the parameters the user sets here. That table on the bottom is generated dynamically and reacts to any changes to the parameters in real time (I’m learning React and want to give it a try here). He only wants a one page app like this (I already did the backend data entry point pretty quickly with ActiveAdmin linked to a Players table that he can access)

The formula looks something like this:
METRIC = ((((KeyStat1KeyStat1Weight)+(KeyStat2KeyStat2Weight)+(KeyStatNKeyStatNWeight))TotalKeyStatWeight) + (RecentFormRecentFormWeight) + (CourseHistoryCourseHistoryWeight))*(Draw)

My hypothesis: The form has a standard ActiveRecord query Player.where(…). Each input field is monitored by React and will update the Player.where(…) query dynamically and update the React component (the table) in real time. I’ll break down parts of the algorithm into methods in the Player model, and the optional ones are only activated if the a certain value is met.

This is the PLAYERS table:

Would you guys recommend any good gems for making complex queries like this? I googled Squirrel where you can make sweet queries like this:

*User.find(:all) do
first_name == “Jon”
email =~ “
created_at >= 4.years.ago
awesomeness <=> (1…10)
banned_since == nil

But I don’t think Thoughtbot supports it anymore. I looked for alternatives like Arel. Are there any good guides to using those gems?

I feel way over my head but I haven’t been able to learn Rails by always being inside my comfort zone. I think I can grow a lot if I manage to pull this off well. He’s waiting for a quote from me and I’m hoping it won’t take me more than a week of concentrated effort. Thanks for the help guys!


I think for the sake of brevity. I may be able to do this all with AJAX calls in rails.

User puts in parameters in a form, submits parameters to controller, controller calls to bits and pieces of the algorithm in the Players model as needed, and renders AJAX partial of results. That would work too right?

I will probably need to write a class like this:

class PlayerFilter

def initialize(scope)
@scope = scope

def filter(params)
filter_by_site params[:site]


def filter_by_site(site)
@scope = @scope.where(‘site LIKE?’, “%#{site}%”)


Probably another class that runs the algorithm on every column in that table before the filter runs.

Just running my thinking process and hypothesis for other developers to share when I approach a Rails project. Hope it helps somebody.


Where can I find that football lineup analyzer? I’m building one myself using the same data and would like to compare notes.

When queries approach this level of complexity, I would consider plain 'ol SQL as an option.

Gems can be handy, but by going down this road you’ll need to invest time in learning a DSL. You might as well learn SQL. SQL isn’t that scary, I promise. :wink:

I actually think SQL is pretty pleasant to use. To me it’s the closest to shouting the computer “GET all the BOOKS in Library WHERE published before ‘1942’”

It’s very human friendly.

There you go! I’m using this as an inspiration. How are you building it out? Are you using ActiveModel or ActiveAttr for a Non-ActiveRecord model that interacts with user input with the DB? That’s how I’m doing it. The only thing that persists is the player’s stats that update every week from the DB.

I would love to compare notes. is my repo if you want to check it out!

I haven’t gotten to far into it, working on the combinatorics portion right now. I’m using ActiveRecord models.

Excellent! Here is my repo. I gotta make a merge from branch to master today. So you may want to check back in a few days.

Are you rolling the solution to the Knapsack problem from stratch or are you using an existing knapsack algorithm? This is so exciting!