Skip to content

Safe Navigation Operator &. don't works on nil objects #531

Open
@madmax

Description

@madmax

Her proxy nil objects and we can't use &.method to protect application to raise exceptions when object is null.

For example we have 2 models Product + Brand

{
  name: "Product"
  brand: null
}

class Product 
  include Her::Model
  belongs_to :brand
end

class Brand
  include Her::Model
end

product = Product.first
product.brand == nil #=> true
product.brand.__id__ == nil.__id__ #=> false

product.brand&.name #=> Exception

NoMethodError: undefined method `name' for nil:NilClass
from /Users/madmax/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/her-1.0.3/lib/her/model/associations/association_proxy.rb:11:in `name'

For temporary solution we use .try method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions