redirect_to next instance on update - ruby-on-rails

I'm trying to redirect users to the next instance of my WordExposition model after update. What I have currently works for immediately-adjacent word_exposition id's, but raises RecordNotFound if the next lesson's word_exposition's ID skips (i.e. it will redirect properly between id's 1-4, but will break if the next id is 6). How can I get it to redirect also for those non-adjacent WordExposition instances that belong to the same lesson?
I based the next_exposition model method on the ideas from this post, but I'm missing something to get it to work here.
WordExposition model:
class WordExposition < ActiveRecord::Base
belongs_to :enrollment
belongs_to :word
def next_exposition
WordExposition.where(["id > ? AND enrollment_id = ?", id, enrollment_id]).first
end
end
WordExpositions controller:
class WordExpositionsController < ApplicationController
def update
current_word_exposition
#current_word_exposition.completed = true
#current_word_exposition.term_given_by_student = params[:word_exposition][:term_given_by_student]
if #current_word_exposition.save
flash[:notice] = "Congratulations!"
#currently only redirects correctly for adjacent words in the same lesson, should do so for non-adjacent word_expositions in the same lesson
if next_word = #current_word_exposition.next_exposition
redirect_to lesson_word_exposition_path(current_lesson, next_word)
end
else
flash[:alert] = "Enter the word exactly as shown!"
redirect_to lesson_word_exposition_path(current_lesson, current_word_exposition)
end
end
private
helper_method :current_lesson
def current_lesson
#current_lesson ||= Lesson.find(params[:lesson_id])
end
helper_method :current_enrollment
def current_enrollment
#current_enrollment ||= Enrollment.find_by!(lesson_id: params[:lesson_id], user_id: current_user.id)
end
def word_exposition_params
params.require(:word_exposition).permit(:completed)
end
helper_method :current_word_exposition
def current_word_exposition
#current_word_exposition ||= current_enrollment.word_expositions.find_by!(word_id: params[:id])
end
end

You can try this
def next_exposition
WordExposition.where('id = (select min(id) from word_expositions where id > ?)', self.id).first
end

Related

going to new page sets my user_id to null in Rails

I have 2 models .. one for User(amitian) and other about
They have a has_one and belongs_to association
Problem is that whenever I create a new about it works fine and set my amitian_id to current_amitian but whenever I go to my new page again.. it updates my amitian_id to NULL
for eg.
this is my sql after I submit the form
insert into about_amitian('values' , amitian_id = 1)
and if i go to new page again it says
update about_amitian set amitian_id = null where about_amitian.id= 1
this is my controller and model
class AboutAmitiansController < ApplicationController
before_action :authenticate_amitian!
def new
#amitian = current_amitian
#about_amitian = #amitian.build_about_amitian
end
def create
#amitian = current_amitian
#about_amitian = #amitian.create_about_amitian(about_amitian_params)
if #about_amitian.save
redirect_to root_url
flash[:notice] = 'success'
else
render root_url
end
end
private
def about_amitian_params
params.require(:about_amitian).permit(:dob,:interest,:bio,:catch_phrase,:relationship_status)
end
end
and model
belongs_to :amitian
and in amitian
has_one :about_amitian
Why is it Updating my database ?

Rails 5 - service class to find users who have email addresses that match an organisation's domain name

I am trying to learn how to use Rails 5 (generally) but specifically, I'm trying to learn how to use service classes.
I'm trying to write a service class that maps a user's given email address (user's have an attribute called :email) to organisation's domain names. Organisations have attributes called :email_format. I use that attribute to hold the part of the email address that follows the "#".
When a user creates an account, I want to take their email address that they use to sign up with, and match the bit after the # to each of the organisations that I know about and try to find a matching one.
My attempts at this are plainly wrong, but I'm struggling to figure out why.
I have resources called User, Organisation and OrgRequest. The associations are:
User
belongs_to :organisation, optional: true
has_one :org_request
Organisation
has_many :org_requests
has_many :users
OrgRequest
belongs_to :user
belongs_to :organisation
I have tried to write a service class as:
class User::OrganisationMapperService #< ActiveRecord::Base
def self.call(user: u)
new(user: user).call
end
def initialize(user: u)
self.user = user
end
def call
if matching_organisation.present?
# user.organisation_request.new(organisation_id: matching_organisation.id)
# user.update_attributes!(organisation_id: matching_organisation.id)
else
#SystemMailer.unmatched_organisation(user: user).deliver_now
end
end
private
attr_accessor :user
def matching_organisation
# User::OrganisationMapperService.new(user).matching_organisation
User::OrganisationMapperService.new(user: user)
end
end
I then have an org requests controller with:
class Users::OrgRequestsController < ApplicationController
before_action :authenticate_user!, except: [:new, :create, :requested]
before_action :set_org_request, only: [:approved, :rejected, :removed]
# skip_before_action :redirect_for_unrequested_organisation
# skip_before_action :redirect_for_unknown_organisation
def index
organisation = Organisation.find_by(owner_id: current_user.id)
return redirect_to(user_path(current_user.id)) if organisation.nil?
#org_requests = organisation.org_requests
end
def new
#all_organisations = Organisation.select(:title, :id).map { |org| [org.title, org.id] }
#org_request = OrgRequest.new#form(OrganisationRequest::Create)
matched_organisation = User::OrganisationMapperService.new(current_user).matching_organisation
#org_request.organisation_id = matched_organisation.try(:id)
end
def create
#org_request = OrgRequest.new(org_request_params)
#org_request.user_id = current_user.id
if #org_request.save
OrgRequest::ProcessService.new(org_request).process
return redirect_to(user_path(current_user),
flash[:alert] => 'Your request is being processed.')
else
# Failure scenario below
#all_organisations = Organisation.select(:title, :id).map { |org| [org.title, org.id] }
render :new
end
end
def requested
# Need help - if this is contained in form inputs - how do i stop from overriding the submit path?
redirect_to(user_path(current_user))
#not sure about this - a similar redirect isnt required for articles or project create
end
def approve
#org_request = current_user.organisation.org_requests.find(params[:id])
if #org_request.state_machine.transition_to!(:approved)
flash[:notice] = "You've added this member."
redirect_to org_requests_path
else
flash[:error] = "You're not able to manage this organisation's members"
redirect_to :index
end
end
def remove
#org_request = current_user.organisation.org_requests.find(params[:id])
if #org_request.state_machine.transition_to!(:removed)
flash[:notice] = "Removed from the organisation."
redirect_to action: :index
# format.html { redirect_to :index }
# format.json { render :show, status: :ok, location: #project }
# redirect_to action: :show, id: project_id
# add mailer to send message to article owner that article has been approved
else
flash[:error] = "You're not able to manage this organisation's members"
redirect_to(user_path(current_user))
# redirect_to action: :show, id: project_id
end
end
def decline
#org_request = current_user.organisation.org_requests.find(params[:id])
if #org_request.state_machine.transition_to!(:declined)
flash[:notice] = "You're not eligible to join this organisation"
redirect_to action: :index
# redirect_back(fallback_location: root_path)
# format.html { redirect_to :index }
# redirect_to action: :show, id: organisation_request.profile
# add mailer to send message to article owner that article has been approved
else
flash[:error] = "You're not able to manage this organisation's members"
redirect_to(user_path(current_user))
# redirect_to action: :show, id: organisation_request.profile
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_org_request
#org_request = OrgRequest.find(params[:id])
authorize #org_request
end
# Never trust parameters from the scary internet, only allow the white list through.
def org_request_params
params.require(:org_request).permit(:organisation_id, :name) # Need help - not sure if I need to put user id and organisation id in this permission
end
end
I can't figure out another approach to this. When I try this, I get this error:
wrong number of arguments (given 1, expected 0)
The error message highlights line 7 of my service class, which has:
def initialize(user: u)
self.user = user
end
I have previously asked questions about this problem here: superclass mismatch for class User - inheriting from ActiveRecord::Base
but I haven't managed to catch the drift of the advice or what is causing the problem. This attempt is a mash up of suggestions that I have gleaned from at least 10 different tutorials - so I appreciate that its highly unlikely to be correct, but I'm struggling to understand how the different parts of this work to know what to try differently.
Can anyone give me a steer on how to try to progress this attempt?
Organisation mapper decorator has:
class User < ActiveRecord::Base
class OrganisationMapper < ::ApplicationDecorator
def matching_organisation
#matching_organisation ||= Organisation.by_email_format(email_format).first
end
def email_format
user.email.split('#').last
end
private
def user
#model
end
end
end
Application decorator has:
class ApplicationDecorator
def initialize(model)
#model = model
end
private
def method_missing(method, *args)
args.empty? ? #model.send(method) : #model.send(method, *args)
end
end
Org request service class has:
class OrgRequest::CreateService < ActiveRecord::Base
attr_accessor :org_request
def self.call(user_id: user_id, organisation_id: org_id)
new(user_id: user_id, organisation_id: organisation_id).call
end
def initialize(user_id: user_id, organisation_id: org_id)
self.user_id = user_id
self.organisation_id = organisation_id
end
def call
self.org_request \
= OrgRequest.new(user_id: current_user.id,
organisation_id: params[:org_request][:organisation_id])
if org_request.save
# send the email
true
else
false
end
end
end
NEXT ATTEMPT
I have tried every variation on this that I can think of. Nothing I'm trying makes any sense to me but I can't make sense out of any examples that I can find.
My service class currently has:
class User::OrganisationMapperService #< ActiveRecord::Base
def self.call(user: u)
new(user: user).call
end
def initialize(user: u)
self.user = user
end
def call
# if matching_organisation.present?
# user.org_request.new(organisation_id: matching_organisation.id)
# if found create a request for that user to enter the organisation
if match_domain.present?
OrgRequest.create(user: #user, organisation_id: #organisation_domain.organisation.id) #if organisation
# user.update_attributes!(organisation_id: matching_organisation.id)
else
#SystemMailer.unmatched_organisation(user: user).deliver_now
end
end
private
attr_accessor :user
# def matching_organisation
# # User::OrganisationMapperService.new(user).matching_organisation
# User::OrganisationMapperService.new(user: user).Organisation.by_email_format(email_format).first
# end
# def matching_organisation
# #matching_organisation ||= Organisation.by_email_format(email_format).first
# end
def user_domain
user.email.split('#').last
end
def organisation_domain
#organisation = Organisation.find_by(email_format: user_domain)
end
# def user_email_domain
# # extract domain from users email
# user_email_domain = #user.email.split('#').last
# end
def match_domain
return unless #user_domain == #organisation.email_format
end
# find an organisation with a matching domain
# end
end
It's plainly wrong. The error message says:
NameError - undefined local variable or method `organisation' for #<User::OrganisationMapperService:0x007faec6ec06b8>
I can't make sense of the error message either because I have put '#' in front of every instance of 'organisation' just to try to make that error go away. It doesn't.
Please help.
ANOTHER COMPLETELY SENSELESS ERROR MESSAGE
I had another go at trying to write the method to check whether an email domain matches an organisation's email format in my service class.
The call method now has:
def call
if user_domain == Organisation.email_format.any?
OrgRequest.create(user: #user, organisation_id: #organisation_domain.organisation.id) #if organisation
else
end
end
The error message in the console says:
NoMethodError - undefined method `email_format' for #<Class:0x007faec72d8ac0>
That has to be nonsense because my organisation table has an attribute in it called :email_format. In the console, I can write:
o = Organisation.first.email_format
Organisation Load (3.3ms) SELECT "organisations".* FROM "organisations" ORDER BY "organisations"."id" ASC LIMIT $1 [["LIMIT", 1]]
That gives me the result I'm looking for.
I'm trying (to my wits end) to learn how rails communicates. I can't make any sense of any of it.
NEXT ATTEMPT
Next guess of a go at the call method:
def call
if user_domain == organisation_domain?
OrgRequest.create(user: #user, organisation_id: #organisation_domain.organisation.id) #if organisation
else
end
Produces this error:
NoMethodError - undefined method `organisation_domain?' for #<User::OrganisationMapperService:0x007faec3be3600>:
I can't seem to find a single form of expression that doesnt produce this error.
The problem appears to be in the following line:
matched_organisation = User::OrganisationMapperService.new(current_user).matching_organisation
It should be this instead:
matched_organisation = User::OrganisationMapperService.new(user: current_user).matching_organisation
I had a session on code mentor. This is the answer. I hope it might help someone else who is trying to learn.
class User::OrganisationMapperService #< ActiveRecord::Base
def self.call(user: u)
new(user: user).call
end
def initialize(user: u)
self.user = user
end
def call
if organisation_domain.present?
OrgRequest.create(user: #user, organisation_id: organisation_domain.id) #if organisation
else
end
end
private
attr_accessor :user
def user_domain
user.email.split('#').last
end
def organisation_domain
#organisation ||= Organisation.find_by(email_format: user_domain)
end
end

rails 4 simple form nested attributes multiple models error during update

I am battling an error with nested attributes and trying to fix the cop error at the same time. So here is the walk through. A coupon code may be submitted with the form using nested attributes that may affect the price of the job. This only occurs if the coupon code is valid. In this scenario the coupon code has already been assigned so the first if coupon_code && coupon.nil? is triggered. When the form comes back around the flash message works correctly but simple form does not display the value. I could adjust simple form to have the value with an instance variable but I'm starting to smell something a bit off here in my logic. Also, the smell of Assignment Branch Condition is starting to worry me. I can move forward with this, but the user would like to see the code. I would too.
Cop Error:
app/controllers/payments_controller.rb:9:3: C: Assignment Branch Condition size for update is too high. [17.97/15]
Controller:
class PaymentsController < ApplicationController
rescue_from ActiveRecord::RecordNotFound, with: :route_not_found_error
Numeric.include CoreExtensions::Numeric::Percentage
def update
#job = Job.find(params[:job_id])
coupon_code = params[:job][:coupon_attributes][:code]
coupon = validate_coupon(coupon_code)
if coupon_code && coupon.nil?
#coupon_code = coupon_code
flash.now[:error] = t('flash_messages.coupons.id.not_found')
render 'payments/new', layout: 'nested/job/payment'
else
update_job(#job, coupon)
update_coupon(coupon, #job) if coupon
redirect_to #job.vanity_url
end
end
def new
#job = Job.find(params[:job_id])
return if reroute?(#job)
render 'payments/new', layout: 'nested/job/payment'
end
private
def update_job(job, coupon)
job.start_at = DateTime.now
job.end_at = AppConfig.product['settings']['job_active_for_day_num'].days.from_now
job.paid_at = DateTime.now
job.price = price_job(coupon)
# job.save
end
def validate_coupon(coupon_code)
return nil unless coupon_code.present?
coupon = Coupon.active.find_by_code(coupon_code)
return nil unless coupon.present?
coupon
end
def price_job(coupon)
price = AppConfig.product['settings']['job_base_price']
return price unless coupon
price = coupon.percent_discount.percent_of(price)
price
end
def update_coupon(coupon, job)
coupon.job_id = job.id
coupon.executed_at = DateTime.now
coupon.save
end
end
View:
ruby:
content_for :body_id_class, 'PaymentNew'
content_for :js_instance, 'viewPaymentNew'
content_for :browser_title, 'Payment'
job_base_price = AppConfig.product['settings']['job_base_price']
coupon_code = #coupon_code ||= ''
= simple_form_for(#job, url: job_payment_path, html: { id: 'payment-processor-form' }) do |j|
div[class='row']
div[class='col-md-12']
div[class='panel panel-default']
div[class='panel-heading']
h3[class='panel-title']
|Total Cost
div[class='panel-body']
h2[class='job-cost' data-initial = "#{job_base_price}"]
= number_to_currency(job_base_price)
div[class='panel-heading']
h3[class='panel-title']
|Have a coupon?
div[class='panel-body']
div[class='row-inline']
div[class='row-block row-block-one']
= j.simple_fields_for :coupon_attributes, #job.coupon do |c|
= c.input_field :code, maxlength: 50, id: 'coupon-code', class: 'form-control', data: { 'initial' => 0 }, value: coupon_code
div[class='row-block']
button[type='button' class='btn btn-primary' id='coupon-verify' ]
|Verify
p[class='help-hint']
= t('simple_form.hints.coupon.code')
div[class='row']
div[class='col-md-12']
= j.button :button, type: 'button', class: 'btn-primary text-uppercase', id: 'purchase-job' do
= job_posting_button_step_label
Updates
Refactoring this code to work with the post below. Factories fixed factorygirl create model association NoMethodError: undefined method
You have quite a few code smells going on in that fat old controller.
Most of them seem to be symtoms that all is not well on the model layer and that you are not modeling the domain very well.
You might want to consider something like this:
class Job < ActiveRecord::Base
has_many :payments
end
class Payment < ActiveRecord::Base
belongs_to :job
belongs_to :coupon
end
class Coupon < ActiveRecord::Base
validates_uniqueness_of :code
end
This will let our countroller focus on CRUD'ing a single resouce rather than trying to herd a bunch of cats.
So lets look at enforcing the business logic for coupons.
class Payment < ActiveRecord::Base
belongs_to :job
belongs_to :coupon
validate :coupon_must_be_active
attr_writer :coupon_code
def coupon_code=(code)
coupon = Coupon.find_by(code: code)
#coupon_code = code
end
private
def coupon_must_be_active
if coupon
errors[:coupon] << "must be active." unless coupon.active?
elsif #coupon_code.present?
errors[:coupon_code] << "is not valid."
end
end
end
The custom attribute writer loads the coupon from the a code. The validation sets up our business logic rules.
We really should do the same when it comes to the job pricing:
class Job < ActiveRecord::Base
after_initialize :set_price
def set_price
self.price ||= AppConfig.product['settings']['job_base_price']
end
end
class Payment < ActiveRecord::Base
after_initialize :set_price
validates_presence_of :job
def net_price
return job.price unless coupon
job.price * (coupon.percent_discount * 00.1)
end
# ...
end
We can then write our controller like so:
class PaymentsController
before_action :set_job
# GET /jobs/:job_id/payments/new
def new
#payment = #job.payments.new
end
# POST /jobs/:job_id/payments
def create
#payment = #job.payments.create(payment_params)
end
# PATCH /jobs/:job_id/payments/:id
def update
#payment = #job.payments.find(params[:id])
end
private
def set_job
#job = Job.find(params[:job_id])
end
def payment_params
params.require(:payment)
.permit(:coupon_code)
end
end
We can then simply setup the form with:
= simple_form_for([#job, #payment]) do |f|
= f.input :coupon_code
= f.submit
Note that you don't want to take the price from the user unless you intend to implement the honor system - you should get it from your models by setting up association callbacks.

in rails how to limit users post count saved in database before asking to upgrade their account

I'm adding a small way of controlling a non-subscribed user and a subscribed user. Basically my idea is that all users that sign up with the use of Devise, get an account. However, my model or the number of posts a user can have in the database stored based on user ID found should be 25 posts. I'm guessing the following would work;
Model
class Post
belongs_to :user
validate :quota, :on => :refresh
def quota
Posts = Posts.find(params[:id])
if user.posts.count >= 25
flash[:error] = "Sorry you need to upgrade"
end
end
end
:refresh is something I'm working on where it grabs posts and adds these posts to the current_user within the database, or assigns the current_user id to each post it adds to the database.
am I correct on the above function? or should I add the validation count into my refresh controller/model like so;
class dashboard
def refresh
...
if self.user.posts.count >= 25
flash[:error] = "You've reached maximum posts you can import"
end
end
end
I would use a before_filter on the corresponding controller(s):
class PostsController < ApplicationController
before_filter :check_quota # you could add here: :only => [:index, :new]
private # optionnal
def check_quota
if user.posts.count >= 25
#quota_warning = "You've reached maximum posts you can import"
end
end
end
And in the view(s):
<% if #quota_warning.present? %>
<span><%= #quota_warning %></span>
<% end %>
Then add the validation on the model, to ensure the constraint:
class Post < ActiveRecord::Base
belongs_to :user
before_save :check_post_quota
private # optionnal
def check_post_quota
if self.user.posts.count >= 25
self.errors.add(:base, "You've reached maximum posts you can import")
return false
end
end
end

Changing a field in the users table after_create of a record satisfies a condition

I want to change a users workout_id in the Users table once he completes his workout. I can't figure out how to do this. Below are the two ways I've tried to figure this out.
Attempt 1: Put an if statement in the controller, that when true increments the users workout_id - The problem is the if statement only passes when the condition is already true, so the user needs to try to complete an additional exercise after his workout is already complete.
Example:
def create
#completed_set = CompletedSet.new(params[:completed_set])
if #totalExercisesInWorkout.included_in?(#completedExercises) == true
raise "it worked!"
end
respond_to do |format|
if #completed_set.save
format.html { redirect_to profile_path, notice: 'Successfully completed set.' }
format.json { render json: #completed_set, status: :created, location: #completed_set }
else
format.html { render action: "new" }
format.json { render json: #completed_set.errors, status: :unprocessable_entity }
end
end
end
Attempt 2 use the after_save callback in the Model. - this doesn't work because I don't have access to the session information in the Model, so I can't know what the user is, what their workout_id is, and if they have finished their workout.
There must be a simple solution to this problem, but I'm totally stumped!
Thanks!
EDIT:
A workout is complete when an array with the exercise_id's of the workout is matched by an array of the completed_set exercise_is's of the user. So when the following code is true:
#totalExercisesInWorkout.included_in?(#completedExercises)
.include_in? is an extension of the Array Class that simply returns true if the second array includes all the values in the first array.
A workout has_many exercises
When a user "completes" an exercise, that exercise and relevant information, such as user_id, workout_id, etc. is stored in the completed_set table.
In my application_controller I have an initialize_vars method that finds all the information I need to know about the user and their workouts. Here it is:
def initialize_vars
#workout = Workout.find(current_user.workout_id)
#user_program = current_user.program
#user_cycle = Cycle.find(current_user.cycle_id)
#user_group = Group.find(current_user.group_id)
#exercise = #workout.exercises
#workout_exercise = #workout.workout_exercises
# Array needs to have an exercise for every set in the exercise
#exercisesInWorkout = #workout.exercises.pluck(:exercise_id)
# Array of the sets in the users current workout
#workoutSets = #workout_exercise.pluck(:sets)
# Array of exercises user has completed in current workout
#completedExercises = CompletedSet.where(:user_id => current_user.id).pluck(:exercise_id)
# Array of exercise_id's in workout times the number of sets for each exercise
#totalExercisesInWorkout = #exercisesInWorkout.zip(#workoutSets).map { |n1, n2| [n1] * n2 }.flatten
#Total number of reps a user has completed
#repsCompleted = CompletedSet.where(:user_id => current_user.id).pluck(:repetitions).sum
#Total amount of weight a user has lifted
#weightLifted = CompletedSet.where(:user_id => current_user.id).pluck(:weight).sum
end
EDIT 2:
I've learned a lot here. I refactored my code. Here is what it looks like now.
Class User
def cycle
Cycle.find(cycle_id)
end
def group
Group.find(group_id)
end
def workout
Workout.find(workout_id)
end
def completed_exercise_ids_array(user)
CompletedSet.where(workout_id: workout_id, user_id: user).pluck(:exercise_id)
end
def sets_in_workout_array
workout.workout_exercises.pluck(:sets)
end
def exercises_in_workout_times_sets_array
workout.workout_exercises.pluck(:exercise_id).zip(sets_in_workout_array).map { |n1, n2| [n1] * n2 }.flatten
end
def update_workout_if_needed!
if exercises_in_workout_times_sets_array.included_in?(completed_exercise_ids_array)
self.workout.id = self.workout.next_workout_id
save!
end
end
# Methods for progress area
def total_reps_completed(user)
CompletedSet.where(user_id: user).sum(:repetitions)
end
def total_weight_lifted(user)
CompletedSet.where(user_id: user).sum(:weight)
end
And my application_controller
class ApplicationController
# Application wide instance variables go here. Just don't forget to call initialize_vars in the controllers you want to call these variables in.
def initialize_vars
#user_workout = current_user.workout
#user_cycle = current_user.cycle
#user_group = current_user.group
#exercise = #user_workout.exercises
# Array of the sets in the users current workout
#workoutSets = current_user.sets_in_workout_array
# Array of exercises user has completed in current workout
#completedExercises = current_user.completed_exercise_ids_array(current_user)
# Array of exercise_id's in workout times the number of sets for each exercise
#totalExercisesInWorkout = current_user.exercises_in_workout_times_sets_array
end
I moved some logic to the profile_controller
An after_create callback for CompletedSet would be the best place for this:
class CompletedSet < ActiveRecord::Base
belongs_to :user
belongs_to :workout
belongs_to :exercise
after_create :update_user_workout
def update_user_workout
user.update_workout_if_needed!
end
end
class User < ActiveRecord::Base
has_many :completed_sets
belongs_to :workout
def completed_exercise_ids
completed_sets.where(workout_id: workout_id).pluck(:exercise_id)
end
def update_workout_if_needed!
if completed_exercise_ids.included_in?(workout.workout_exercises.pluck(:exercise_id))
self.workout = Workout.next(workout)
save!
end
end
end
class Workout < ActiveRecord::Base
has_many :workout_exercises
has_many :exercises, through: :workout_exercises
# some logic to determine the next workout - modify as needed
def self.next(workout)
where("workouts.sequence > ?", workout.sequence).order(:sequence).first
end
end
This solution assumes that you have associated the current_user with the CompletedSet in your controller create action (which I'm not seeing):
def create
#completed_set = current_user.completed_sets.build(params[:completed_set])
...

Resources