Rails 5 - beta Attribute API

Rails-5-0-beta1:

 Rails-5.0-beta1 is available after tenth anniversary of Rails 1.0. still there are apps ruunning in production
 based on Rails-5.0-beta1 for Example Basecamp 3.

Attributes API

Your models are getting a new attribute class method in Rails 5, allowing you to easily define a relationship
between the model and a non-Active Record type. No more misusing serialize!

{% highlight ruby %}

# before
# The type detected by Active Record can be overridden.

    # db/schema.rb
    create_table :store_listings, force: true do |t|
        t.decimal :price_in_cents
    end
    
    # app/models/store_listing.rb
    class StoreListing < ActiveRecord::Base
    end
    
    store_listing = StoreListing.new(price_in_cents: '10.1')
    
    store_listing.price_in_cents # => BigDecimal.new(10.1)
    
    class StoreListing < ActiveRecord::Base
        attribute :price_in_cents, :integer
    end
    
    store_listing.price_in_cents # => 10

# after
# Attributes do not need to be backed by a database column.

    class MyModel < ActiveRecord::Base
        attribute :my_string, :string
        attribute :my_int_array, :integer, array: true
        attribute :my_float_range, :float, range: true
    end
    
    model = MyModel.new(
        my_string: "string",
        my_int_array: ["1", "2", "3"],
        my_float_range: "[1,3.5]",
    )
    model.attributes
    # =>
    {
        my_string: "string",
        my_int_array: [1, 2, 3],
        my_float_range: 1.0..3.5
    }

{% endhighlight %}

And if you get stuck… Ask Here

email me rajeevsharma86@gmail.com

Avatar
Rajeev Sharma
Technical Lead

software entrepreneur and father of a fairy tale!!!.