Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails check if model is empty

I created model Profile.rb
I have on the db column name, Currently it's empty
How do I check on the rails console if Profile is empty?
I have tried Profile.empty? but I guess that's not the right way

like image 449
user4571629 Avatar asked Mar 08 '16 02:03

user4571629


People also ask

How do you check if an object is empty in rails?

Specifically, if the object responds to #empty? , they will check whether it is empty. If you go to http://api.rubyonrails.org/ and search for "blank?", you will see what objects it is defined on and how it works.

Is empty string nil in Ruby?

nil? will only return true if the object itself is nil. That means that an empty string is NOT nil and an empty array is NOT nil.

Does null exist in Ruby?

Let's start out with “Nil” since it's the most common and easy-to-understand way of representing nothingness in Ruby. In terms of what it means, Nil is exactly the same thing as null in other languages. So, what is null? Null, as it exists in many languages, is a sort of value that represents “nothing”.


1 Answers

This also could be an Option for you.

Profile.any?

This will make a query to DB like

Profile.any?
# (0.6ms)  SELECT COUNT(*) FROM "profiles"
# => false

This would be more semantic I guess.

like image 152
illusionist Avatar answered Oct 03 '22 02:10

illusionist