Destroy all issue

41 views Asked by At

I have a standard has_many/belongs_to relationship with movies and posters. Each movie can have many posters. I have the following in a rake command:

task remove_movie_posters: :environment do
   destroyed = Movie.where(fake: true).posters.where("created_at < ?", 1.minute.ago).destroy_all
   puts "Done. " + destroyed.length + " records deleted."
end

but I get the error: TypeError: no implicit conversion of Fixnum into String

1

There are 1 answers

0
spickermann On BEST ANSWER

destroy_all returns an integer and you cannot concatinate strings and integers like that. A simple example to show this problem is:

"foo" + 42 + "bar"
TypeError: no implicit conversion of Fixnum into String

The solution is to use string interpolation (or the to_s method, but that feels ugly to me):

puts "Done. #{destroyed.length} records deleted."