Web Development

Having fun with ActiveRecord.

class Account < ActiveRecord::Base
  attr_accessible :name,

Getting attributes of a new ActiveRecord object

rails console
[1] pry(main)> Account.new.attributes
=> {"id"=>nil,

We get blanks on most properties without default values.

Getting attributes of an existing ActiveRecord object

Account Load (39.3ms)  SELECT "accounts".* FROM "accounts" LIMIT 1
=> {"id"=>1,
 "name"=>"Golden Arches",
 "notes"=>"Golden Arches FTW!",
 "created_at"=>Mon, 02 Dec 2013 09:52:57 UTC +00:00,
 "updated_at"=>Mon, 02 Dec 2013 09:52:57 UTC +00:00,
 "photo_updated_at"=>Mon, 02 Dec 2013 09:52:57 UTC +00:00,

We get all the properties of the existing record.

Attributes when only certain fields are selected

  Account Load (0.5ms)  SELECT name FROM "accounts" LIMIT 1
=> {"name"=>"Golden Arches"}

Only the selected fields are received by the attributes method.