Delete with Ruby on Rails
Hello, I am newer to rails and I am having a issues with deleting some records in a mysql database. The problem started when I try to delete two items (Lists) and (Todos) the todos are related to the lists so when you delete a list I need the todos to be deleted. This part is working good, but when I try to delete a list with NO todos it gives me an error telling me I have a nil object.
So in trying to fix this I have come up with what I have below. The way I have works BUT I still get an error (The error occurred while evaluating nil.all_hashes). I have checked the MySQL database and the data is gone so it works fine but I just keep getting this error. How can I fix this and do it in a more proper manner then what I have?
def delete
List.find(params[:id]).destroy
@num = Todo.find_by_list_id(params[:id])
if @num == nil
redirect_to :action => "dash"
else
Todo.find_by_sql("DELETE FROM todos WHERE list_id='#{params[:id]}%'")
redirect_to :action => "dash"
end
end
