0

I have my show action as below:

def show
  @car = Car.friendly.find(params[:id])
  @cars = @car.other_cars_in_showroom
end

My Car model with appropriate method is:

def other_cars_in_showroom
  cars = self.showroom.cars
  cars.delete(self)
  cars
end

My issue is, the above works and the page renders fine, however, after a page refresh I get:

undefined method `cars' for nil:NilClass

The error is on the self.showroom.cars line. How can I resolve this? All I require is to retrieve a list of all other cars in the same showroom as @car but then to remove the currently display car from the list so as to reduce the duplication.

On my view I am taking @cars and rendering this view for each of them:

<div class="car__photo">
  <div class="car__photo-inner">
    <%= image_tag car.s3_photo(:thumb), class: 'styled-picture' %>
  </div>
</div>

<div class="car__info">
  <h4 class="car__info__name"><%= car.name %></h4>
  <p class="car__info__park"><%= car.park_name %></p>
</div>

<%= link_to car.name + ' at ' + car.showroom_name, car_path(car), class: 'car__link' %>

My expected output is for @cars to be a list of all the cars in the same showroom as @car but without @car itself.

rctneil
  • 7,016
  • 10
  • 40
  • 83
  • Can you please add input and the expected output. Please share your show view also. – Kirti Thorat Mar 17 '14 at 22:29
  • I'm not sure what input you wish me to provide but I have rewritten my question as I rejigged a few things around to move more to my Car model and I still get similar errors. Any ideas? – rctneil Mar 18 '14 at 17:10

1 Answers1

1

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
Kirti Thorat
  • 52,578
  • 9
  • 101
  • 108
  • Why would the .delete remove that car from my database? Surely it will just delete it from the Collection Array of @cars? That's what I intended anyway. Also why remove the other_cars_in_showroom method? Surely it's best to have that logic in my model so I can use it elsewhere? – rctneil Mar 19 '14 at 17:55
  • Try this on `rails console`. `c=Car.first` then `c.showroom.cars.delete(c)`. You will see delete qauery being fired. – Kirti Thorat Mar 19 '14 at 18:11
  • If you wish to keep the logic in model you can easily convert and move the `@car.showroom.cars.where.not(id: @car.id)` part in some other method. – Kirti Thorat Mar 19 '14 at 18:12
  • Ok, Will give that a go shortly. Thankyou! – rctneil Mar 19 '14 at 18:21
  • I am eager to know whether my answer resolved your query or not? :) – Kirti Thorat Mar 20 '14 at 22:27
  • My apologies, Only had a short amount of time to try it out in since I have been back at home. Yes, it worked perfectly! – rctneil Mar 20 '14 at 22:34