Rails: In Rails how to use the model’s Attribute API on a tableless model

You need to include ActiveModel::Attributes

class SomeModel
  include ActiveModel::Model
  include ActiveModel::Attributes
  attribute :foo, :integer, default: 100
end

For some reason its not included in ActiveModel::Model. This internal API was extracted out of ActiveRecord in Rails 5 so you can use it with table-less models.

Note that ActiveModel::Attributes is NOT the same thing as ActiveRecord::Attributes. ActiveRecord::Attributes is a more specialized implementation that assumes the model is backed by a database schema.