Rails Model method check if field nil, if so return empty string

Chris Valentine

In my model class I have a method:

def full_address
    address_lines = [self.address1,self.address2,self.address3].reject!(&:empty?).join(',')
    fulladdress = address_lines + ", " + self.city + ', ' + self.state  + ', ' + self.zipcode
    return fulladdress 
end

This is just an easy way to return all the address fields to view them quickly.

This returns undefined method 'empty?' for nil:NilClasswhen its empty. How can I change the method to work if all address1, address2, address3 are empty?

I guess I'm looking for a function like:

ReturnBlankIfNilElseValue(self.address1)

Anything like that exist?

def full_address
    address_lines = [self.address1.to_s,self.address2.to_s,self.address3.to_s].reject!(&:empty?).join(',')
    fulladdress = address_lines + ", " + self.city + ', ' + self.state  + ', ' + self.zipcode
    return fulladdress 
end

This variation returns no implicit conversion of nil into String which is odd because in rails console i can type nil.to_s and get an empty string.

I don't really want to check if each field is nil and then add to the array, i'd prefer a function that just returned empty string if it is nil.

Sorry the nil.to_s does work its the 2nd line that needed one as well. Its working now will get rid of this.

Myst

You can, although this might not be what you wanted, simply remove any nil objects from the address Array using the #compact method, so that #empty? will work...:

def full_address
    [self.address1, self.address2, self.address3,
        self.city, self.state, self.zipcode].compact.reject!(&:empty?).join(', ')
end

You should be aware that this WILL return a partial address if SOME of the fields exist.

I would probably void any fault addresses (in this example I require just the first address line and the city fields):

def full_address
    return "" unless self.address1 && self.city && !self.address1.empty? && !self.city.empty? 
    [self.address1, self.address2, self.address3,
        self.city, self.state, self.zipcode].compact.reject!(&:empty?).join(', ')
end

Disregarding the few edits I made to the code, I would probably go with @Joseph's #present? together with the validation, since you're using Rails:

def full_address
    return "" unless self.address1 && self.city && self.address1.present? && self.city.present? 
    [self.address1, self.address2, self.address3,
        self.city, self.state, self.zipcode].reject!(&:present?).join(', ')
end

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails check if model is empty

From Java

Check if not nil and not empty in Rails shortcut?

From Dev

Rails active model serializer, how to return empty string instead of null

From Dev

Rails active model serializer, how to return empty string instead of null

From Dev

Should Ruby method return nil or empty object

From Dev

Rails define nil check on record in model

From Dev

rails accessing model attributes return nil

From Dev

rails text_field / text_ara empty string vs nil

From Dev

Check if a string isn't nil or empty in Lua

From Dev

undefined method `[]' for nil:NilClass for Rails model on new

From Dev

undefined method `[]' for nil:NilClass for Rails model on new

From Dev

return a default value for empty field on a django model

From Dev

Using Rails model method to fetch and return value

From Dev

Rails 4: model.where.not does not return nil records

From Dev

Is the empty string a legal field/method name?

From Dev

Why does NSDateFormatter not return nil when given empty string ("")

From Dev

Why does NSDateFormatter not return nil when given empty string ("")

From Dev

Why does my new class method in a Rails model result in nil?

From Dev

Rails undefined method 'model_name' for nil:nilClass with nested attributes

From Dev

Rails simple form undefined method `model_name' for nil:NilClass

From Dev

Rails: undefined method `model_name' for nil:NilClass

From Dev

Rails simple form undefined method `model_name' for nil:NilClass

From Dev

Rails - NoMethodError: undefined method `each' for nil:NilClass on Model.find_by

From Dev

Rails 4.1 tests return undefined method `authenticate' for nil:NilClass

From Dev

Rails: return capitalized string from model

From Dev

Check if input field value exist in array, if so return to page with an error

From Dev

Check a recordset for an empty field

From Dev

Check if BCC field is empty

From Dev

Check if calendar field is empty

Related Related

  1. 1

    Rails check if model is empty

  2. 2

    Check if not nil and not empty in Rails shortcut?

  3. 3

    Rails active model serializer, how to return empty string instead of null

  4. 4

    Rails active model serializer, how to return empty string instead of null

  5. 5

    Should Ruby method return nil or empty object

  6. 6

    Rails define nil check on record in model

  7. 7

    rails accessing model attributes return nil

  8. 8

    rails text_field / text_ara empty string vs nil

  9. 9

    Check if a string isn't nil or empty in Lua

  10. 10

    undefined method `[]' for nil:NilClass for Rails model on new

  11. 11

    undefined method `[]' for nil:NilClass for Rails model on new

  12. 12

    return a default value for empty field on a django model

  13. 13

    Using Rails model method to fetch and return value

  14. 14

    Rails 4: model.where.not does not return nil records

  15. 15

    Is the empty string a legal field/method name?

  16. 16

    Why does NSDateFormatter not return nil when given empty string ("")

  17. 17

    Why does NSDateFormatter not return nil when given empty string ("")

  18. 18

    Why does my new class method in a Rails model result in nil?

  19. 19

    Rails undefined method 'model_name' for nil:nilClass with nested attributes

  20. 20

    Rails simple form undefined method `model_name' for nil:NilClass

  21. 21

    Rails: undefined method `model_name' for nil:NilClass

  22. 22

    Rails simple form undefined method `model_name' for nil:NilClass

  23. 23

    Rails - NoMethodError: undefined method `each' for nil:NilClass on Model.find_by

  24. 24

    Rails 4.1 tests return undefined method `authenticate' for nil:NilClass

  25. 25

    Rails: return capitalized string from model

  26. 26

    Check if input field value exist in array, if so return to page with an error

  27. 27

    Check a recordset for an empty field

  28. 28

    Check if BCC field is empty

  29. 29

    Check if calendar field is empty

HotTag

Archive