Sylar Sylar - 3 months ago 6
Ruby Question

Check if false then run code

I want to test something. If

false
do this. I wish not to test for true, only false. My code looks like this and I only need to do something if false:

if Model.exists?(table_column: 'joe')
# this part is blank
else
# run this code
end


My code runs after the
else
which is what I want. Is there a clean rails/ruby method that tests for false? My code would only run if false, not true.

If duplicate, I'll gladly delete. Thanks

RSB RSB
Answer

Use unless

unless Model.exists?(table_column: 'joe')
  # run code
end

or if not

if not Model.exists?(table_column: 'joe') 
  # run code
end

or

if Model.find_by(table_column: 'joe').blank?
  # run code
end

Hope that helps!