I’m a beginner at RoR and I am toying around with a database that has 6000 rows of data. Since there is so much data to begin with, I have the index table displaying only rows of data that have unique item names by using this in the controller/index view.
@glyphs_test = Glyph.group(:item)
Alongside each row, I want to display the number of times that particular item appears in the entire database.
When I try this, it works but I don’t know how to print it out onto the index.
Glyph.group(:item).uniq.count
results with, for example:
=> {"Eternal Fire" => 8, "Glyph of Adrenaline Rush" => 74} etc...
Even if I was able to print out the number only in this, how can I match it up with the table? I think I am missing something crucial here. I am able to do this in the individual record by using this in the show#controller but not in index#controller.
@glyph = Glyph.find(params[:id])
@glyphs = Glyph.where(["item = ?", @glyph.item]).order('net_gain ASC')
Thank you in advance for any advice. Perhaps I should do something more simpler but there must be a solution to this.
You can iterate through your results hash with something like the following
Or if you are iterating through your glyphs and trying to get the count from the results, you could do the following