# Introduction to Rails 5 Attributes

DevFeed: [Introduction to Rails 5 Attributes](<https://devfeed.tech/articles/introduction-to-rails-5-attributes-21055.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2015/12/20/rails-5-attributes/>)

Published: 2015-12-20T12:00:00Z

Content type: article

Language: en

Sources: [Jake Yesbeck](<https://devfeed.tech/sources/jake-yesbeck.md>)

Topics: [Rails](<https://devfeed.tech/topics/rails.md>), [Persistence](<https://devfeed.tech/topics/persistence.md>), [Code](<https://devfeed.tech/topics/code.md>), [Transactions](<https://devfeed.tech/topics/transactions.md>)

Tags: [activerecord](<https://devfeed.tech/tags/activerecord.md>), [migration](<https://devfeed.tech/tags/migration.md>), [persistence](<https://devfeed.tech/tags/persistence.md>), [rails](<https://devfeed.tech/tags/rails.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [ruby-on-rails](<https://devfeed.tech/tags/ruby-on-rails.md>), [sql](<https://devfeed.tech/tags/sql.md>), [transactions](<https://devfeed.tech/tags/transactions.md>)

## AI overview

This article introduces ActiveRecord Attributes in Rails 5. The feature lets developers declare an attribute's type and optional default value, providing explicit type coercion for model attributes. The article demonstrates converting a string-backed success field to a boolean without changing a large database column, and discusses using typed attributes for both persisted and object-lifecycle data.

## Source excerpt

Shortly after the tenth anniversary of Ruby on Rails 1.0, Rails 5.0 Beta has been announced. While the main character of this release was without a doubt ActionCable, other really great features have made their debut. Types of Changes One feature that particularly stood out is the introduction of ActiveRecord Attributes. This feature allows a developer to assert a specific type for a given attribute and an optional default value. It is not strict type validation (which I have a very strong affinity for), but it does define explicit type coercion that can be very useful. Given an example application that deals with Transactions, a single table and model might exist: class CreateTransactions < ActiveRecord::Migration def change create_table :transactions do |t| t.integer :user_id t.string :item_name t.integer :quantity t.string :success t.decimal :price t.timestamps(null: false) end end end class Transaction < ActiveRecord::Base end This structure, like most in the wild, could have been created before all edge cases were thought through. For some reason, success is a String instead of a Boolean. While this problem might seem trivial, imagine a system where hundreds of millions of transactions exist. In such a system the entire column might not be able to change without incurring downtime. This is a perfect example problem that Attributes can help remedy. Starting simple, a single line can be added to the Transaction class definition to coerce success to a boolean: class Transaction < ActiveRecord::Base attribute :success, :boolean end Using the same logic that ActiveRecord uses for database column coercion, we can see attributes in action! transaction = Transaction.new(success: 'yes') transaction.success # => true transaction = Transaction.new(success: 'f') transaction.success # => false transaction = Transaction.new(success: 0) transaction.success # => false Just like that, the schema has been improved. Aside from raw SQL update statements, this code now prevents str