开发者

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? }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜