On the Rails Guide, I saw a class was defined as
class Person < ActiveRecord::Base
end
and then it wrote
>>p = Person.new(:name => "John Doe")
=>#<Person id: nil, name: "John Doe", created_at: nil, :updated_at: nil
Since class Person has no field, why here it is valid to pass a :name attribute to the constructor? Shouldn't there be some error with it?
Also, why id, name, created_at all have no colon but :updated_at has one?
Because your class inherits from ActiveRecord::Base
and ActiveRecords looks at your database and dynamically creates attributes that match the columns in the corresponding database table.
updated_at
doesn't have a column either. Please look again carefully. It must be a mistake.
To see the fields defined in an ActiveRecord class you need to look at the db/schema.rb
file. That will show you the fields that have been created through migrations.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.