Simple Rails 3 question: How to check if some product has a description?
Each Product
has several fields, including description
which is a string (if there is no description the开发者_开发知识库n description=""
).
What is the easiest method to find out if there is at least one Product
with non empty description ?
non_empty_description_exist = Product.<what should be here?>
Product.count(:conditions => "description IS NOT NULL")
if null value is allowed. It'll return the total count of Products with description available.
If you need the first product with description per se, use first instead of count
Product.all.any? { |product| product.description.present? }
精彩评论