I have the following relations:
class Patient < ActiveRecord::Base
belongs_to :user
has_many :analyses, dependent: :destroy
end
class Analysis < ActiveRecord::Base
belongs_to :patient
end
To use scopes in Patient is simple, I just did:
class Scope < Scope
def resolve
if user.admin?
scope.all
else
scope.where(user_id: user.id)
end
end
end
But how can I use do it to get all the analysis that only belongs to one specific patient using the resolve method?
Right now my analysis_policy look like this:
class AnalysisPolicy < ApplicationPolicy
def new?
true
end
def edit?
record.patient.user_id == user.id
end
alias_method :show?, :edit?
alias_method :create?, :edit?
alias_method :destroy?, :edit?
end
And the index action in AnalysesController:
def index
@analyses = @patient.analyses
end
...
private
def set_patient
@patient = Patient.find(params[:patient_id])
end