Rails object update - check if any attribute is nil

jordan

I would like to be able to check if any of my object's attributes are nil when updating them so that I can display them to my users as a "draft" or "complete." Is there a way to check this? Or is there a better way of implementing functionality for this?

I have a bool attribute in the table called :complete. I'd like to change this to true or false, depending on it's state. Or, is there a way to check all attributes in the view for a nil attribute, and then remove the :complete attribute?

Santhosh

To check if any of the object's attributes are nil, you can do

@object.attributes.values.include?(nil)

or

@object.attributes.values.any? &:nil?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

check if any property in an object is nil - Swift 3

rails 5 update attribute only if it's currently nil

Boolean Attribute Update with Devise and Rails' Check Box Helper Method

Check if single attribute in managedObjectContext is *not* nil

rails object is nil

Check if any field of struct is nil

How set up and call a rails method in the view to update an attribute of an object

How to check if `Any(not Any?)` is nil or not in swift

Check if not nil and not empty in Rails shortcut?

How to check object is nil or not in swift?

Cannot update any attribute in the testUpdateBookOrdersShippingAddress

Check if `Any` value is object

Update attribute only when value is not nil

Ruby on rails return a string if a model attribute is nil

Rails find record where attribute is nil

Any difference between nil check left on right

Rails 3 check if attribute changed

How to check that object with same ID (or any other attribute ) exists in array of objects?

How do I check to see if specific attribute changed upon rails update method?

Rails devise after_sign_in first time, check attribute and update another user one time

How to update Model attribute in Rails

Ruby on Rails: undefined method `any?' for nil:NilClass

Best way to check for nil and update hash value

check if parent record has any child records with start_date in the future then stop update rails

Check to find out if any file has been added to file_field before update, Ruby on Rails 7

Rails define nil check on record in model

Check if object attribute in an array is unique

Object attribute Null/Empty check

Julia - Check object has attribute