jeudi 24 octobre 2019

iterate through elements in array within active record

I am trying to do a comparison and return a statement whether the item is inside of the array or not. Menu is an object that I have in active record which is composed of 3 attributes, the menu type, meal and the restaurant id. For the meal attribute I had a variable set to it which is actually an array that contains the names of meals.

In my code when I do puts"#{menu.meal}" I get back...

["Pancakes W/ Eggs and Bacon", "Bacon Egg and Cheese", "Oatmeal W/ Raisins", "Scrambled Eggs W/ Grits", "Blueberry Waffles W/ Syrup", "Chocolate Chip Pancakes W/ Sausage", "Yogurt Muffin"]

which is the array of meals that I have set in my seeds file. In the code below what I am trying to do is when the user enters the meal that it wants I want to search through all the meals if it exits, and if it does then the user can proceed and if not I want to output an error message saying that it does not exist.

 puts "What meal would you like to order"
 item_meal = gets.chomp


 menus_meals = res.menus.select do |menu|
   binding.pry
   menu.meal == item_meal

Aucun commentaire:

Enregistrer un commentaire