Read more

Pitfall: has_defaults on virtual attributes are nil when loaded from database, of course …

Deleted user #6
September 04, 2015Software engineer

It smells. Rethink your code design.

Illustration online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
Read more Show archive.org snapshot

Code example with makandra/has_defaults Show archive.org snapshot :

class Post < ActiveRecord::Base

  has_defaults tags: []          # field in db
  has_defaults virtual_tags: []  # no db field
  
  def all_tags
    virtual_tags + tags
  end
  
end

> Post.new.virtual_tags
=> []   # ✔

> Post.find(1).virtual_tags
=> nil   # ☹

> Post.find(1).all_tags
=> Error: undefined method '+' for nil:NilClass
Posted to makandra dev (2015-09-04 17:37)