简体   繁体   English

Rails Model方法检查字段是否为nil,如果是,则返回空字符串

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

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:NilClass 这将返回undefined method 'empty?' for nil:NilClass undefined method 'empty?' for nil:NilClass when its empty. undefined method 'empty?' for nil:NilClass为空。 How can I change the method to work if all address1, address2, address3 are empty? 如果所有地址1,地址2,地址3都为空,如何更改工作方式?

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. 这种变化no implicit conversion of nil into String返回no implicit conversion of nil into String这很奇怪,因为在Rails控制台中,我可以键入nil.to_s并获取一个空字符串。

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. 我真的不想检查每个字段是否为nil,然后将其添加到数组中,我更喜欢一个函数,如果它为nil,则只返回空字符串。

Sorry the nil.to_s does work its the 2nd line that needed one as well. 抱歉,nil.to_s确实也需要第二行。 Its working now will get rid of this. 现在可以正常工作了。

You can use present? 您可以使用present? to check for nil without throwing an error (it returns false for nil and for empty string or a string with just spaces in it). 检查nil而不抛出错误(对于nil和空字符串或其中仅包含空格的字符串,它返回false。

So, instead of using reject and empty? 那么,不是使用rejectempty? , you can use select and present? ,您可以使用selectpresent? .

    address_lines = [self.address1,self.address2,self.address3].select(&:present?).join(',')

This won't return an empty string for blank or nil addresses -- it will just leave them out of address_lines. 这不会为空地址或零地址返回空字符串-只会使它们不在address_lines中。

Note present? 注意present? is the opposite of blank? blank?的对立面blank? suggested by chad_. 由chad_建议。 I think either one will help you get the result you need. 我认为任何一种都可以帮助您获得所需的结果。

Decent summary on this blog: http://railsless.blogspot.com/2011/08/difference-between-nil-empty-blank.html 这个博客的体面总结: http : //railsless.blogspot.com/2011/08/difference-between-nil-empty-blank.html

In rails, you can check foo.blank? 在rails中,您可以检查foo.blank? and it will check nil? || empty 它会检查nil? || empty nil? || empty

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? 您可以,尽管这可能不是您想要的,但只需使用#compact方法从地址Array中删除所有nil对象,这样#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? 不管我对代码进行的几处编辑,我可能都会选择#present?#present? together with the validation, since you're using Rails: 与验证一起使用,因为您使用的是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM