Hey, thanks in advance for the help.
I have another pretty straight forward question.
This isn’t very rails-like, but it works. Is there a way to do it better? I know there’s group_by, for dates, but I can’t figure out how to use it for this.
I’m still super new at ruby, but know enough to tell that there must be a better way!
def self.getRecipeNames
recipes = Recipe.all
names = Hash.new
recipes.each do |recipe|
names[recipe.id] = recipe.name
end
names
end
Thanks!
You can use
reduce(injectin older version of Ruby) to transform your list of recipes to single hash (that’s why it is reduce, it reduces to single value, which can be list/hash/whatever):