Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
madmax opened this issue Nov 28, 2019 · 0 comments
Open

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

madmax opened this issue Nov 28, 2019 · 0 comments

Comments

@madmax
Copy link

madmax commented Nov 28, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant