Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>Shirt.uniq.pluck(:fabric)</code> returns a <code>Relation</code>. Therefore it will never be <code>nil</code>.</p> <p>This <code>Relation</code> defines parts of a sql query. This says: <em>Give me (all|one of the) unique values of fabric in the database.</em> To actually run that query, you need to call a method on that relation that triggers the database call: <code>all</code>, <code>first</code>, <code>each</code>, <code>any?</code>, <code>blank?</code> ... </p> <p>Through the lack of context, I do not know how you use your <code>Shirt</code> model. But I guess the query will never give you the expected answer. Because <code>Shirt.uniq.pluck(:fabric)</code> will always return something as long there is at least one row in that table. Imagine there is only one row in the table and it's <code>fabric</code> is <code>nil</code>, than <code>Shirt.uniq.pluck(:fabric).blank?</code> would determine <code>Shirt.uniq.pluck(:fabric)</code> to <code>[nil]</code>. And <code>[nil].blank? == false</code></p> <p>If you work on one specific shirt, use <code>@shirt.fabric.present?</code> If you want to know if there is at least one shirt in the db without a fabric <code>Shirt.where(fabric: nil).any?</code></p> <p>I recommend to read:</p> <ul> <li><a href="http://guides.rubyonrails.org/active_record_querying.html" rel="nofollow">http://guides.rubyonrails.org/active_record_querying.html</a></li> <li><a href="http://api.rubyonrails.org/classes/ActiveRecord/Relation.html" rel="nofollow">http://api.rubyonrails.org/classes/ActiveRecord/Relation.html</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload