Rails Model

Why does the generated rails model doesn’t have attributes? How does
the rails know what attributes a model has?

In railstutorials.org says that the ActiveRecord scans the columns of
the associated table of the model and thru that it can know the
attributes a model has. Is there more info about this?

maiah wrote in post #1019231:

Why does the generated rails model doesn’t have attributes? How does
the rails know what attributes a model has?

$ rails generate model User name:string email:string

See those things after User???

$ bundle exec rake db:migrate

That creates the db from the “migration” created by ‘rails generate
model’. Here’s what a migration looks like:

class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.string :email

  t.timestamps
end

end

def self.down
drop_table :users
end
end

In railstutorials.org says that the ActiveRecord scans the columns of
the associated table of the model and thru that it can know the
attributes a model has. Is there more info about this?

rails calls attr_accessor() in your model for all the columns in the db.
Also, rails adds an id column and two timestamp columns.

On Tue, Aug 30, 2011 at 8:39 AM, maiah [email protected] wrote:

In railstutorials.org says that the ActiveRecord scans the columns of
the associated table of the model and thru that it can know the
attributes a model has. Is there more info about this?

Yes, the Rails documentation :slight_smile:

I’d suggest starting with ActiveRecord::Base.


Hassan S. ------------------------ [email protected]

twitter: @hassan

Thanks! Now I get it. But I prefer to put Annotations so that I can
see which attributes a Model has rather cross-checking the table.

On Aug 31, 12:36am, Hassan S. [email protected]