简体   繁体   中英

array iteration for ruby on rails

Each favorite consists of the customer id and restaurant id. I am basically trying to iterate through all the favorites and I am trying to see if the current customer.id is equal to the customer_id of the instance and then return it. Im not sure if select was the right iterator I should have used... I am confused as to the next steps I should take. In the end I will have to find a way to get the names of the restaurant which I have an idea how to do but for now I need to find a way just to get the instances of the favorites first that belong to the specific customer I am working with and have them saved into a variable so I can then later on do another iteration through them to get the names of the restaurants. I used a binding.pry but it seems like the customer_favs is coming out to be nil...

def view_all_favorites(customer)
  customer_favs = Favorite.all.select do |cus_favs|
    cus_favs.customer_id == customer.id
    binding.pry
  end
end

It appears a Customer has_many Favorite 's right? In that case, ActiveRecord gives you this for free via customer.favorites

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM