I created a Ruby on Rails application (2.3.9) where users can record workouts. I am trying to implement a ‘star’ system which would allow a logged_in user to star workouts from other users that they want to remember.
I am doing this by creating a Star resource (associations below). I am successfully creating a deleting records in my Star table and now want to conditionally display view code based on if a current_user has already starred a workout.
I am displaying all workouts through a partial for _workouts.html.erb. For each workout, I would like to either display the <% remote_form_for [workout, Star.new]... or You have already starred this workout.
Workout :has_many => :stars
User :has_many => :stars
Star :belongs_to => :workouts
Star :belongs_to => :users
I imagine I will have to create a named_scope in Star.rb that looks to see if if there is an existing star record where the workout_id matches workout.id and where current_user.id matches the star.user_id match for that workout. It confuses me just typing this. Please help.
You can create a
has_many ..., :through => ...relationship, and then use#include?to check for an association.