Ok, j'ai un modèle générique TimeSlot
qui traite un start_at
et un end_at
pour les plages de temps. Un couple de modèles en dérivent, mais je fais référence à un dans cette question: AppointmentBlock
qui est une collection de Appointments
. Je veux valider un AppointmentBlock
tel qu'aucun autre AppointmentBlocks
n'ait été programmé pour un Employee
particulier dans le même laps de temps. Depuis AppointmentBlock
a une association polymorphique avec TimeSlot
, vous devez accéder à travers le TimeSlot
comme l » start_at
et end_at
le AppointmentBlock
donc: appt_block.time_slot.start_at
Cela signifie que je dois avoir une sorte de se joindre à mon :conditions
pour mon appel find()
méthode. Voici mon code à ce jour:Rails: utilisation de la méthode find pour accéder aux tables jointes pour les relations polymorphiques
#inside my time_slot.rb model
belongs_to :time_slot_role, :polymorphic => true
#inside my appointment_block.rb model
has_one :time_slot, :as => :time_slot_role, :dependent => :destroy
validate :employee_not_double_booked
def employee_not_double_booked
unless self.employee_id
# this find's condition is incorrect because I need to join time_slots to get access
# to start_at and end_at. How can I do this?
blocks = AppointmentBlock.find(:first,
:conditions => ['employee_id = ? and (start_at between ? and ? or end_at between ? and ?)',
self.employee_id, self.time_slot.start_at, self.time_slot.end_at,
self.time_slot.start_at, self.time_slot.end_at])
# pseudo code:
# collect a list of appointment blocks that end after this
# apointment block starts or start before this appointment
# block ends that are also associated with this appointment
# blocks assigned employee
# if the count is great then 0 the employee has been double
# booked.
# if a block was found that means this employee is getting
# double booked so raise an error
errors.add "AppointmentBlock",
"has already been scheduled during this time" if blocks
end
end
Depuis AppointmentBlock
n'a pas start_at
ou un end_at
comment puis-je joindre à la table time_slots
pour obtenir ces conditions de travail?
Merci William et bienvenue à Stackoverflow! J'ai oublié des informations importantes sur ma relation polymorphe (voir mon article édité). Je pense que pour faire ce travail, je vais avoir besoin de '' rejoindre time_slots sur time_slots.time_slot_role_id = rendez-vous_blocks.id'' – DJTripleThreat
si vous modifiez votre réponse en utilisant la partie que je viens de commenter, j'accepterai votre réponse. – DJTripleThreat
Modification du code pour refléter la structure de votre table. Heureux d'avoir pu aider. – William