DRY with_options
ruby activesupport railsOne of the cool features in ActiveSupport
is with_options
.
It helps you remove duplication:
Will become:
However, there is one gotcha which was unclear for me after reading apidock.
When using ActiveRecord associations you have to use block variable to define associations like in example above. So this:
Will not work! However, when using validations, delegations, attr_* it’s not required and you can write this:
Conclusion
Use with_options
, but remember to add block variable when using it for ActiveRecord associations.