Вопрос

Is it possible to use a association in my slug_candidates method?

for example, where my model belongs_to :car

def slug_candidates
    [
      [car.owner, :horsepower, "horsepower"],
      [car.owner, car.nickname, :horsepower, "horsepower"]
    ]
end

right now it returns

NoMethodError: undefined method `owner' for nil:NilClass

Это было полезно?

Решение

I assume this is a method defined in an ActiveRecord model.

It's because one of the records you are using does not have a car (ie. it is nil).

So when you do car.owner on that specific record (there may be more than one btw) are essentially trying to call the method owner on nil (ie. nil.owner), therefore the error.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top