Ok, so I’ve got this object @categories which is basically:
@categories = Category.all
passed into a page by a controller. So from here I want to grab the category field of the record with ID 12 (which is culled from another variable called currentCatId). So I do this:
currentCatId = 12
currentCat = @categories.find(currentCatId)
Wrong, this gives me all of the categories! Which I can’t understand. So looking around I found that I could try something like this:
currentCat = @categories.find { |cat| cat.id = currentCatId }.category
This at least retrieves the field that I want, but not for the category with ID 12, but rather for the first category.
I’m going crazy here, can’t understand why the instanced object has a .find method (that doesn’t seem to work) but not a .where method.
What am I doing wrong and what’s the correct way to do this?
As meagar says you should use
==to compare. But anyway, what’s wrong with the simple O(1) find: