active record in rails 5

14
Active Record in Rails 5 http://jyaasa.com Copyright 2017. Jyaasa Technologies.

Upload: jyaasa-technologies

Post on 16-Feb-2017

62 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Active record in rails 5

Active Record in Rails 5

http://jyaasa.comCopyright 2017. Jyaasa Technologies.

Page 2: Active record in rails 5

Hello ! I am Neha Suwal

http://jyaasa.comCopyright 2017. Jyaasa Technologies.

Associate Software Engineer Jyaasa Technologies

Page 3: Active record in rails 5

http://jyaasa.comCopyright 2017. Jyaasa Technologies.

Instance Public methods:

attribute(name, cast_type, **options)

Defines an attribute with a type on this model. It will override the type of existing attributes if needed. This allows control over how values are converted when assigned to a model.

attribute :price_in_cents, :integer

Page 4: Active record in rails 5

http://jyaasa.comCopyright 2017. Jyaasa Technologies.

name: The name of the methods to define attribute methods for, and the column which this will persist to.

cast_type: A symbol such as :string or :integer, or a type object to be used for this attribute.

options:

default - The default value to use when no value is provided. If this option is not passed, the previous default value (if any) will be used. Otherwise, the default will be nil.

array (PostgreSQL only)- specifies that the type should be an array (see the examples below).

range (PostgreSQL only)- specifies that the type should be a range (see the examples below).

Page 5: Active record in rails 5

http://jyaasa.comCopyright 2017. Jyaasa Technologies.

The type detected by Active Record can be overridden

#generate a model named StoreDetail with a column price_in_cents whose data type is decimal

class StoreDetail < ApplicationRecordend

create_table :store_details do |t| t.decimal :price_in_cents

t.timestampsend

store_detail = StoreDetail.create(price_in_cents: '10.1')store_detail.price_in_cents #=>

#<BigDecimal:585ab10,'0.101E2',18(18)>

Page 6: Active record in rails 5

http://jyaasa.comCopyright 2017. Jyaasa Technologies.

#in rails 5class StoreDetail < ApplicationRecord

attribute :price_in_cents, :integerend

store_detail.price_in_cents #=> 10

Page 7: Active record in rails 5

http://jyaasa.comCopyright 2017. Jyaasa Technologies.

A default can also be provided

create_table :store_details do |t| t.decimal :price_in_cents

t.string :name, default: "original default" t.timestampsend

store_detail = StoreDetail.create(price_in_cents: '10.1')store_detail.name #=> “original default”

Page 8: Active record in rails 5

http://jyaasa.comCopyright 2017. Jyaasa Technologies.

#in rails 5class StoreDetail < ApplicationRecord

attribute :name, :string, default: "new default"end

new_store_detail = StoreDetail.create(price_in_cents: '1000')new_store_detail.name #=> "new default"

Page 9: Active record in rails 5

http://jyaasa.comCopyright 2017. Jyaasa Technologies.

Attributes do not need to be backed by a database column

class StoreDetail < ApplicationRecordattribute :price_in_cents, :integerattribute :name, :string, default: "new default"attribute :time, :datetime, default: -> { Time.now }

end

StoreDetail.new.time #=> Mon, 02 Jan 2017 04:48:24 UTC +00:00

Page 10: Active record in rails 5

http://jyaasa.comCopyright 2017. Jyaasa Technologies.

class StoreDetail < ApplicationRecordattribute :price_in_cents, :integerattribute :name, :string, default: "new default"attribute :time, :datetime, default: -> { Time.now }attribute :field, :integer, array: true

end

store_detail = StoreDetail.new(field: ["1", "2", "3"])store_detail.field #=> [1, 2, 3]

Page 11: Active record in rails 5

http://jyaasa.comCopyright 2017. Jyaasa Technologies.

store_detail.attributes #=> {"id"=>nil, "price_in_cents"=>nil, "name"=>"new default", "created_at"=>nil, "updated_at"=>nil, "time"=>Mon, 02 Jan 2017 04:52:25 UTC +00:00, "field"=>[1, 2, 3]}

Page 12: Active record in rails 5

http://jyaasa.comCopyright 2017. Jyaasa Technologies.

Summary:

● The type detected by Active Record can be overridden.● A default can also be provided.● Attributes do not need to be backed by a database column.

Page 13: Active record in rails 5

References

http://edgeguides.rubyonrails.org/5_0_release_notes.htmlhttp://api.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.html

http://jyaasa.comCopyright 2017. Jyaasa Technologies.

Page 14: Active record in rails 5

Thank You!Any Queries? Lets Discuss

http://jyaasa.comCopyright 2017. Jyaasa Technologies.