Trying to implement an create if not exists else update record in Active Record.
Currently using:
@student = Student.where(:user_id => current_user.id).first
if @student
Student.destroy_all(:user_id => current_user.id)
end
Student = Student.new(:user_id => current_user.id, :department => 1
)
Student.save!
What would be the correct way of updating the record if it exists or else create it?
You may be looking for
first_or_createor something similar:http://guides.rubyonrails.org/v3.2.17/active_record_querying.html#first_or_create