Skip to content

Instantly share code, notes, and snippets.

@nfilzi
Created August 16, 2018 13:03
Show Gist options
  • Save nfilzi/586f7aa9175304c06a81fab5515f86b4 to your computer and use it in GitHub Desktop.
Save nfilzi/586f7aa9175304c06a81fab5515f86b4 to your computer and use it in GitHub Desktop.
class Rapper < ApplicationRecord
belongs_to :agent, class_name: "User"
has_many :bookings
end
class User < ApplicationRecord
has_many :rappers, foreign_key: :agent_id
has_many :bookings
# source: -> pour spécifier à AR qu'il doit aller chercher la relation bookings du rapper comme source pour les reservations
# tu pourras donc faire ensuite current_user.reservations,
# et avoir accès à tous les bookings posés sur les rappers appartenant au current_user
has_many :reservations, through: :rappers, source: :bookings
end
class Booking < ApplicationRecord
belongs_to :user
belongs_to :rapper
# Scopes pour la répartition des bookings dans le controller :)
scope :past, -> { where("ending_on < ?", Date.current) }
scope :accepted, -> { where(status: 'accepted') }
scope :pending, -> { where(status: 'pending') }
end
class Agent::BookingsController < ApplicationController
def index
# Ici tu cherches juste à choper les bookings qui portent sur tous les rappers de l'agent (current_user)
# @all_bookings = Booking.all
# @id = current_user.id
# @bookings = []
# @all_bookings.each do |booking|
# if booking.rapper.agent.id == current_user.id
# @bookings << booking
# end
# end
# Tu peux maintenant utiliser la relation reservations (voir modèles)
@bookings = current_user.reservations
# Ici tu cherches à séparer les bookings de ton agent en fonction de certains critères
# Si tu définis des scopes sur ton modèle Booking, tu peux les utiliser ici:
@past_bookings = @bookings.past
@accepted_bookings = @bookings.accepted
@pending_bookings = @bookings.pending
# @past = []
# @accepted = []
# @pending = []
# @bookings.each do |booking|
# if booking.ending_on < Date.current
# @past << booking
# elsif booking.status == "pending"
# @pending << booking
# else booking.status == "accepted"
# @accepted << booking
# end
# end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment