Active Record in Rails 5
http://jyaasa.comCopyright 2017. Jyaasa Technologies.
Hello !
I am Neha Suwal
http://jyaasa.comCopyright 2017. Jyaasa Technologies.
Associate Software Engineer
Jyaasa Technologies
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
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).
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 < ApplicationRecord
end
create_table :store_details do |t|
t.decimal :price_in_cents
t.timestamps
end
store_detail = StoreDetail.create(price_in_cents: '10.1')
store_detail.price_in_cents #=> #<BigDecimal:585ab10,'0.101E2',18(18)>
http://jyaasa.comCopyright 2017. Jyaasa Technologies.
#in rails 5
class StoreDetail < ApplicationRecord
attribute :price_in_cents, :integer
end
store_detail.price_in_cents #=> 10
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.timestamps
end
store_detail = StoreDetail.create(price_in_cents: '10.1')
store_detail.name #=> “original default”
http://jyaasa.comCopyright 2017. Jyaasa Technologies.
#in rails 5
class StoreDetail < ApplicationRecord
attribute :name, :string, default: "new default"
end
new_store_detail = StoreDetail.create(price_in_cents: '1000')
new_store_detail.name #=> "new default"
http://jyaasa.comCopyright 2017. Jyaasa Technologies.
Attributes do not need to be backed by a database column
class StoreDetail < ApplicationRecord
attribute :price_in_cents, :integer
attribute :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
http://jyaasa.comCopyright 2017. Jyaasa Technologies.
class StoreDetail < ApplicationRecord
attribute :price_in_cents, :integer
attribute :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]
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]}
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.
References
http://edgeguides.rubyonrails.org/5_0_release_notes.html
http://api.rubyonrails.org/classes/ActiveRecord/Attributes/ClassMethods.ht
ml
http://jyaasa.comCopyright 2017. Jyaasa Technologies.
Thank You!
Any Queries? Lets Discuss
http://jyaasa.comCopyright 2017. Jyaasa Technologies.

Active record in rails 5

  • 1.
    Active Record inRails 5 http://jyaasa.comCopyright 2017. Jyaasa Technologies.
  • 2.
    Hello ! I amNeha Suwal http://jyaasa.comCopyright 2017. Jyaasa Technologies. Associate Software Engineer Jyaasa Technologies
  • 3.
    http://jyaasa.comCopyright 2017. JyaasaTechnologies. 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
  • 4.
    http://jyaasa.comCopyright 2017. JyaasaTechnologies. 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).
  • 5.
    http://jyaasa.comCopyright 2017. JyaasaTechnologies. 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 < ApplicationRecord end create_table :store_details do |t| t.decimal :price_in_cents t.timestamps end store_detail = StoreDetail.create(price_in_cents: '10.1') store_detail.price_in_cents #=> #<BigDecimal:585ab10,'0.101E2',18(18)>
  • 6.
    http://jyaasa.comCopyright 2017. JyaasaTechnologies. #in rails 5 class StoreDetail < ApplicationRecord attribute :price_in_cents, :integer end store_detail.price_in_cents #=> 10
  • 7.
    http://jyaasa.comCopyright 2017. JyaasaTechnologies. A default can also be provided create_table :store_details do |t| t.decimal :price_in_cents t.string :name, default: "original default" t.timestamps end store_detail = StoreDetail.create(price_in_cents: '10.1') store_detail.name #=> “original default”
  • 8.
    http://jyaasa.comCopyright 2017. JyaasaTechnologies. #in rails 5 class StoreDetail < ApplicationRecord attribute :name, :string, default: "new default" end new_store_detail = StoreDetail.create(price_in_cents: '1000') new_store_detail.name #=> "new default"
  • 9.
    http://jyaasa.comCopyright 2017. JyaasaTechnologies. Attributes do not need to be backed by a database column class StoreDetail < ApplicationRecord attribute :price_in_cents, :integer attribute :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
  • 10.
    http://jyaasa.comCopyright 2017. JyaasaTechnologies. class StoreDetail < ApplicationRecord attribute :price_in_cents, :integer attribute :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]
  • 11.
    http://jyaasa.comCopyright 2017. JyaasaTechnologies. 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]}
  • 12.
    http://jyaasa.comCopyright 2017. JyaasaTechnologies. 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.
  • 13.
  • 14.
    Thank You! Any Queries?Lets Discuss http://jyaasa.comCopyright 2017. Jyaasa Technologies.