开发者

Simple Rails 3 question: How to check if some product has a description?

开发者 https://www.devze.com 2023-01-29 22:56 出处:网络
Each Product has several fields, including description which is a string (if there is no description the开发者_开发知识库n 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? }
0

精彩评论

暂无评论...
验证码 换一张
取 消