I’m trying to check if a find method returns a result. My find method is the following:
post = Post.find(:all, :conditions => { :url => params['url'] }, :limit => 1)
What would be a good way to check that post contains a result?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
find :allreturns an empty array ([]) if no rows are returned, so you can just use it this way:By the way, if you do
find :allyou’re going to get an array, not a single row. If you’re trying to get just one Post, it would be cleaner to use thefind_byhelper orfind :firstor justfirstinstead: