You get undefined method 'cars' for nil:NilClass
error because cars.delete(self)
is actually deleting the current car from database.
def other_cars_in_showroom
cars = self.showroom.cars
cars.delete(self) ## Fires delete query for self(deletes current car)
cars
end
Update the show action as below, to get all the other cars in the showroom of the selected car except the selected car:
def show
@car = Car.friendly.find(params[:id])
@cars = @car.other_cars_in_showroom
end
Update the other_cars_in_showroom
as below:
def other_cars_in_showroom
showroom.cars.where.not(id: self.id)
end