I just discovered this handy little gem called annotate. Run it and it’ll insert a comment in all of your model files at once, outlining the schema. This is incredibly convenient when working with your Rails models to be able to see the data and types right there in the top of the file.
Here’s what my Noober (/app/models/noober.rb) model looks like by default:
class Noober < ActiveRecord::Base end
Spartan as always. Let's give ourselves something to look at with the annotate command. First, include annotate in your Rails 3 project in the Gemfile:
group :development do ... gem 'annotate' end
Run the install command to update your gems:
$ bundle install
Run it from within your project directory:
$ bundle exec annotate --position before Annotated (1): Noober
And the result should look something like this in your models:
# == Schema Information # # Table name: noobers # # id :integer not null, primary key # firstname :string(255) # lastname :string(255) # email :string(255) # nooblevel :integer # created_at :datetime # updated_at :datetime # class Noober < ActiveRecord::Base end
Awesome! Now I can see quickly which parts of the model I need to work with and add my code without having to peek at the database:
# == Schema Information
#
# Table name: noobers
#
# id :integer not null, primary key
# firstname :string(255)
# lastname :string(255)
# email :string(255)
# nooblevel :integer
# created_at :datetime
# updated_at :datetime
#
class Noober < ActiveRecord::Base
attr_accessible :firstname, :lastname, :email
validates :firstname, :presence => true
validates :lastname, :presence => true
validates :email, :presence => true
end
Works well for me!