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

Leave a Reply




Created by DesignForWeb company. All rights reserved © 2007-2010. Check also the iPhone / iPad developers blog
Disclaimer
The materials collected in this blog were taken from open access sources. We try our best to preserve the copyrights of original authors and clearly state the authorship as well as link to original source website where it's possible. Please leave your comment if you feel offended by any post or if you dispose of any information about breach of copyright law in this blog. We will do our best to resolve the situation immediately.