Are we asking the right question?
It would certainly be interesting to know the earliest location where we can get a monster with a given ability, but we have access to more data than that, so we could actually answer a bigger question with the same effort. How about, "what are the locations where I can get a monster with the Auto-Bravery ability, sorted from earliest to latest?" We still need to find the earliest location where a tamable monster has the Auto-Bravery ability, but we can find out more this way. We've transformed the question from a linear search through the locations from earliest to latest into a filter-and-sort query, which is easy for a database to do.Sorting by Location Depth
class MonsterController < ApplicationController
def index
if params[:filter]
location = Location.find_by(name: params[:filter])
@monsters = location.location_monsters +
location.location2_monsters +
location.location3_monsters
elsif params[:ability_filter]
ability = Ability.find_by(name: params[:ability_filter])
@monsters = ability.get_all_monsters.sort_by { |monster| monster.first_location_depth }
elsif params[:skill_filter]
ability = RoleAbility.find_by(name: params[:skill_filter])
@monsters = ability.get_all_monsters.sort_by { |monster| monster.first_location_depth }
else
@monsters = Monster.all
end
end
def show
@monster = Monster.find(params[:id])
end
endIt ends up reading in code just about the same as it reads in the above description. I went ahead and did the sort on both abilities and skills here because it's the same code. Now, we need to define this first_location_depth method that we've called on the monster model. As long as it returns the depth of the location where the monster can first be found, sort_by will end up sorting the list of monsters the way we want it to. Here's one way to define that method in app/models/monster.rb:class Monster < ApplicationRecord
# ... A whole mess of belongs_to macros ...
def first_location_depth
[location&.depth, location2&.depth, location3&.depth].compact.min
end
endWe simply build up a short array of depths using the existence (&.) operator for each potential location, compact the array to remove the nil values for the locations that didn't exist, and return the minimum value. That's all there is to it. When we click on the Auto-Bravery ability in the ability table, we are presented with a list of monsters with that ability sorted by the depth of the first location where we can find them:What's Left?
Since we already have the feature of finding strong monsters in each location, the only things remaining are the two tables that we haven't connected: monster materials and monster characteristics. Monster materials is an interesting table that we can use to figure out how quickly we can level up the monsters we've tamed because we need to find the monsters that drop those materials that are used to level up our tamed monsters. However, enabling that feature is going to take a bit of work because the material table doesn't currently have the info for which monsters drop each material. We'll add that missing data and connect up that table next time.











