Posted over 5 years ago. Visible to the public.

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

It smells. Rethink your code design.

Code example with makandra/has_defaults:

Copy
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

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Martin Straub
Last edit:
over 5 years ago
by Martin Straub
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Martin Straub to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more