I thought I had defined the method, but I keep getting an error. Should
I just be able to call user.favorite_places? Would appreciate any
help.
favorite_places_controller.rb
class FavoritePlacesController < ApplicationController
before_action :set_place, except: [:index]
before_action :authenticate_user!
def index
@places = User.favorite_places
end
def create
if Favorite.create(favorited: @place, user: current_user)
redirect_to @place, notice: ‘Place has been favorited’
else
redirect_to @place, alert: ‘Something went wrong. womp womp’
end
end
def destroy
Favorite.where(favorited_id: @place.id, user_id:
current_user.id).first.destroy
redirect_to @place, notice: ‘Place is no longer a favorite’
end
private
def set_place
@place = Place.find(params[:place_id] || params[:id])
end
end
Favorite.rb
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :favorited, polymorphic: true
belongs_to :place
end
user.rb
class User < ActiveRecord::Base
#Relationships
#Favorites
has_many :favorites
has_many :favorite_places, through: :favorites, source: :favorited,
source_type: ‘Place’
end