0

For customer_favs I was able to save all the instances of a favorite for the current customer I am working with. The favorite for a customer consists of the customer id and restaurant id. With that I decided to iterate through it and also iterate through all of the restaurants and see if the id of that the restaurant instance is the same as the restaurant_id of the favorite and return it which gives me an array of favorites.

Now what I am trying to do is find a way to return the instance of the restaurant through the favorite which contains the restaurant_id, I just want to get the restaurant's name. I decided to iterate again though the array of favorites and iterate through the restaurants and compare to see if the restaurant_id of the favorites is the same as the one of the instances of the restaurant and have those instances saved inside of the variable but I am getting an error saying " undefined method `restaurant_id' ".

def view_all_favorites(customer)
  customer_favs = customer.favorites

  get_res_id = customer_favs.each do |fav_res|
    Restaurant.all.select do |res|
      res.id == fav_res.restaurant_id
    end
  end

  get_res = Restaurant.all.select do |res|
    get_res_id.restaurant_id == res.id
  end

  puts "#{get_res}"
end
tadman
  • 208,517
  • 23
  • 234
  • 262
hannah
  • 195
  • 1
  • 12
  • `each` always returns the object being iterated. That's not what you want here. Additionally `Restaurant.all` fetches *every* restaurant in the database regardless of if it matches or not and is really inefficient. Try and use `where` queries. – tadman Oct 19 '19 at 00:14
  • It's hard to tell what's going on here but you may be over-complicating things. What you probably want is `customer.favorites.restaurants` if `Favorite` has a `belongs_to :restaurant` and `Customer` has a `has_many :restaurants, through: :favorites`. – tadman Oct 19 '19 at 00:16
  • @tadman, I tried doing so and I got an error saying undefined method `restaurants' – hannah Oct 19 '19 at 00:58
  • Make sure you did `has_many :restaurants` and not `has_many restaurants` as the missing colon makes it appear to be a method call. – tadman Oct 21 '19 at 19:19

1 Answers1

0

The problems mentioned in the comments are significant, but here's a light edit to get it working:

def view_all_favorites(customer)
  customer_favs = customer.favorites # get favorites from customer

  get_res_id = customer_favs.map do |fav_res| # get restaurant IDs from favorites
    fav_res.restaurant_id
  end

  get_res = Restaurant.all.select do |res| # get restaurants from their IDs
    get_res_id.include?(res.id)
  end

  res_names = get_res.map do |res| # get names from restaurants
    res.name
  end

  puts "#{res_names}"
end

That could probably be simplified to something like this:

def view_all_favorites(customer)
  favorite_restaurants = Restaurant.where(id: customer.favorites.map(&:restaurant_id))
  puts favorite_restaurants.map(&:name)
end

But, as tadman said, it's preferable to set up relations so you don't even have to do that.

  • and for the second code can you explain to me what the & is for? – hannah Oct 19 '19 at 02:00
  • `restaurants.map(&:name)` is the same as `restaurants.map{ |r| r.name }`. For more details see: https://stackoverflow.com/questions/9429819/what-is-the-functionality-of-operator-in-ruby – Luke Persola Oct 19 '19 at 02:07