Skip to main content
The Bike Shed

487: ActiveModel custom attributes

37 min episode · 2 min read
·

Episode

37 min

Read time

2 min

AI-Generated Summary

Key Takeaways

  • Custom Attribute Pattern: Store primitive values like strings or integers in the database, but access them as rich objects with domain methods. Phone numbers stored as strings become PhoneLib objects with formatting, validation, and country detection capabilities.
  • Query Integration: Custom attributes enable querying with rich objects instead of raw values. Pass a PhoneLib object directly into ActiveRecord where clauses without worrying about format matching, as the attribute handles serialization automatically during database operations.
  • Normalization Strategy: Implement database normalization in custom attributes to enable reliable querying and indexing. Store phone numbers in E164 format or percentages as decimals consistently, while presenting user-friendly formats in views without scattered conversion logic throughout the codebase.
  • Abstraction Benefits: Consolidate scattered helper methods and conversion logic into single attribute classes. Replace repeated divide-by-100 and times-100 operations for percentages with domain objects that handle display formatting, mathematical operations, and storage transparently across multiple models.

What It Covers

Joelle and Sally explore ActiveModel custom attributes in Rails, examining how to create rich objects that serialize to database primitives while maintaining domain logic, with examples including phone numbers, money, and percentages.

Key Questions Answered

  • Custom Attribute Pattern: Store primitive values like strings or integers in the database, but access them as rich objects with domain methods. Phone numbers stored as strings become PhoneLib objects with formatting, validation, and country detection capabilities.
  • Query Integration: Custom attributes enable querying with rich objects instead of raw values. Pass a PhoneLib object directly into ActiveRecord where clauses without worrying about format matching, as the attribute handles serialization automatically during database operations.
  • Normalization Strategy: Implement database normalization in custom attributes to enable reliable querying and indexing. Store phone numbers in E164 format or percentages as decimals consistently, while presenting user-friendly formats in views without scattered conversion logic throughout the codebase.
  • Abstraction Benefits: Consolidate scattered helper methods and conversion logic into single attribute classes. Replace repeated divide-by-100 and times-100 operations for percentages with domain objects that handle display formatting, mathematical operations, and storage transparently across multiple models.

Notable Moment

Sally realizes she has been using custom attributes through the MoneyRails gem without understanding the underlying pattern, discovering how this approach eliminates the mental overhead of remembering which format percentages use in different database columns.

Know someone who'd find this useful?

You just read a 3-minute summary of a 34-minute episode.

Get The Bike Shed summarized like this every Monday — plus up to 2 more podcasts, free.

Pick Your Podcasts — Free

Keep Reading

More from The Bike Shed

We summarize every new episode. Want them in your inbox?

Similar Episodes

Related episodes from other podcasts

This podcast is featured in Best Cybersecurity Podcasts (2026) — ranked and reviewed with AI summaries.

You're clearly into The Bike Shed.

Every Monday, we deliver AI summaries of the latest episodes from The Bike Shed and 192+ other podcasts. Free for up to 3 shows.

Start My Monday Digest

No credit card · Unsubscribe anytime