简化Ruby:仅当对象不为nil时才基于对象属性进行更新

时间:2023-01-25 22:30:17
if SOMETHING
  charge = Object (this object has a method ID)
end

DiffObject.update_attributes(specific_attribute: charge.id)

But obviously, if the SOMETHING did not evaluate to true then I don't need to update anything, or in my case, I think it might be easier to just run an update_attributes with specific_attribute:nil

但显然,如果SOMETHING没有评估为true,那么我不需要更新任何内容,或者在我的情况下,我认为使用specific_attribute运行update_attributes可能更容易:nil

So what I have right now is this:

所以我现在拥有的是:

DiffObject.update_attributes(specific_attribute: ((defined? charge) == nil ? nil : charge.id)))

But something tells me there's a MUCH better way of doing it

但有些东西告诉我,有更好的方法可以做到这一点

2 个解决方案

#1


0  

ActiveSupport's core extension provides a handy method try on almost every object.

ActiveSupport的核心扩展为几乎每个对象提供了一个方便的方法。

try(*a, &b)
Invokes the public method whose name goes as first argument just like public_send does, except that if the receiver does not respond to it the call returns nil rather than raising an exception.

try(* a,&b)调用名称作为第一个参数的公共方法,就像public_send一样,除了如果接收者没有响应它,则调用返回nil而不是引发异常。

For example, you can use change.try(:id). If change is nil or something that doesn't respond to id, it returns nil, otherwise it returns the ID.

例如,您可以使用change.try(:id)。如果change为nil或者没有响应id的内容,则返回nil,否则返回ID。

See RailsGuide and Rails API doc for details.

有关详细信息,请参阅RailsGuide和Rails API文档。

#2


0  

When you don't know whether charge is nil, you can use try to safely access the id. It will return the id if charge is present, and just return nil if the charge is nil, without an error.

当您不知道费用是否为零时,您可以尝试安全地访问该ID。如果电荷存在,它将返回id,如果电荷为零,则返回nil,没有错误。

DiffObject.update_attributes(specific_attribute: charge.try(:id))

The methods update_attributes, update, save etc will only save the record if it has actually changed, so you don't need to check that yourself. Just save it and let ActiveRecord figure out the rest.

update_attributes,update,save等方法只会在实际更改后保存记录,因此您无需自行检查。只需保存它,让ActiveRecord弄清楚其余部分。

#1


0  

ActiveSupport's core extension provides a handy method try on almost every object.

ActiveSupport的核心扩展为几乎每个对象提供了一个方便的方法。

try(*a, &b)
Invokes the public method whose name goes as first argument just like public_send does, except that if the receiver does not respond to it the call returns nil rather than raising an exception.

try(* a,&b)调用名称作为第一个参数的公共方法,就像public_send一样,除了如果接收者没有响应它,则调用返回nil而不是引发异常。

For example, you can use change.try(:id). If change is nil or something that doesn't respond to id, it returns nil, otherwise it returns the ID.

例如,您可以使用change.try(:id)。如果change为nil或者没有响应id的内容,则返回nil,否则返回ID。

See RailsGuide and Rails API doc for details.

有关详细信息,请参阅RailsGuide和Rails API文档。

#2


0  

When you don't know whether charge is nil, you can use try to safely access the id. It will return the id if charge is present, and just return nil if the charge is nil, without an error.

当您不知道费用是否为零时,您可以尝试安全地访问该ID。如果电荷存在,它将返回id,如果电荷为零,则返回nil,没有错误。

DiffObject.update_attributes(specific_attribute: charge.try(:id))

The methods update_attributes, update, save etc will only save the record if it has actually changed, so you don't need to check that yourself. Just save it and let ActiveRecord figure out the rest.

update_attributes,update,save等方法只会在实际更改后保存记录,因此您无需自行检查。只需保存它,让ActiveRecord弄清楚其余部分。