Posted by
flyerhzm
on
January 23, 2011
Are you tired of going to schema.rb to find your table structures information? It would be better to list all the attributes of the model in the model itself.
Before
The fact that rails dynamically creates the model attributes at runtime saves repetitive typing, but I find it difficult to discover what attributes exist on model class. I have to go to schema.rb to find the table structure information. For example, I have two models Post and Comment,
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
The two models are defined in post.rb and comment.rb files, from these two files, it is impossible to know what attributes the Post and Comment models have. It may not be a big problem when you create and define the two models, but what about the other developers? They don't know anything about these two models when they first read these two models. They have to go to the schema.rb to discover what attributes.
ActiveRecord::Schema.define(:version => 20101223141603) do
create_table "posts", :force => true do |t|
t.string "title"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
create_table "comments", :force => true do |t|
t.text "body"
t.integer "post_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
end
After reading the schema.rb, we know the Post model has attributes title and body, and the Comment model has attributes body.
I'm tired of always going to the schema.rb to find what attributes the models have, it wastes my time, of course yours, why not list them in the models themselves?
After
Some other model frameworks such as datamapper, mongomaper and mongoid directly define the attributes in the models, I really prefer their manner. As ActiveRecords uses migration/schema.rb to manage the attributes of models, we can't define the attributes in models directly, but we can drop the table structures information in models in the form of comments. Here I highly recommend annotate gem, it will automatically add the comments at the top or bottom of your models to list the table structures information. The following are the results after annotate works.
# == Schema Information
#
# Table name: posts
#
# id :integer(4) not null, primary key
# title :string(255)
# body :text(16777215)
# created_at :datetime
# updated_at :datetime
# user_id :integer(4)
#
class Post < ActiveRecord::Base
end
# == Schema Information
#
# Table name: comments
#
# id :integer(4) not null, primary key
# body :text(16777215)
# post_id :integer(4)
# user_id :integer(4)
# created_at :datetime
# updated_at :datetime
#
class Comment < ActiveRecord::Base
end
It's pretty amazing, I never need to go to the schema.rb anymore. It's an intuitive manner to discover all the attributes of the models.

Comments
https://github.com/miyucy/annotate_models
Add this line to your Gemfile:
Personally I don't see the problem with dropping into MySQL and having a quick look at the schema. Quite the contrary in fact. The database is not some far away system that we must try to ignore and create layers around. It's the core of your app so you should spend time with it and get familiar with the schema!
I would prefer taking this approach all the way. That ActiveRecord could read a schema definition, a DSL probably, in the models looking something like what we see in many NoSQL models. Mongoid has its field definitions, Ripple calls the same thing properties...
But then I would also like rails to be able to use changes to these in simple automatic migrations so that I would only have to create real migrations files for the very few more complicated schema changes. (yes, I realise the many difficulties in making this work... I can dream, can't I?)
Hobo does just what was requested here, i.e., using hobo_fields, you put
all the table information in the models. See below:
http://www.hobocast.com
http://cookbook.hobocentral.net/manual/hobofields
Hobo makes Rails even easier, and more fun, to use.
https://github.com/openteam/annotated_models
If using TextMate, you can also high light class name and press " ctrl + shift + cmd + s", this will show database schema for current class. (will take some time)
Or just press "option + space" for showing attributes.
The problem with this approach is we have extra comment to maintain and you know that comments lies (most of the time)
rake db:migrateit by dropping this in your projects Rakefile: