orjan
orjan

Reputation: 1482

Creating readable models in rails

I have just started with Rails and coming from a .net background I find the model inheriting from ActiveRecord is hard to understand, since the don't contain the corresponding attributes for the model. I cannot imagine a new developer exposed to a large code where the models only contains references to other models and business logic.

From my point of view the DataMapper model is much easier to grasp but since ActiveRecord is the defacto standard it feels weird to change the ORM just for this little problem.

DataMapper

class Post
  include DataMapper::Resource

  property :id,         Serial    # An auto-increment integer key
  property :title,      String    # A varchar type string, for short strings
  property :body,       Text      # A text block, for longer string data.
  property :created_at, DateTime  # A DateTime, for any date you might like.
end

ActiveRecord

 class Post < ActiveRecord::Base
 end

I'm not sure if this is an issue and that people get used to the models without attributes, or how does experienced rails user handle this?

I don't think using the database manager or looking at loads of migrations scripts to find the attributes is an option?

Specifying attr_accessible will make the model more readable but I'm not sure if it's a proper solution for my problem?

Upvotes: 0

Views: 153

Answers (3)

zetetic
zetetic

Reputation: 47548

A few tips:

  • Load up the Rails console and enter Post.column_names for a quick reminder of the attribute names. Post.columns gives you the column objects, which shows the datatypes

  • db/schema.rb contains all the migration code in one place, so you can easily see all the column definitions.

  • If you are using a decent editor/IDE there should be a way to allowing you to jump from the model file to the migration file. (e.g. Emacs with ROR or Rinari)

Upvotes: 2

grifaton
grifaton

Reputation: 4046

You don't have to "look at loads of migration scripts to find the attributes" - they're all defined in one place in db/schema.rb.

Upvotes: 2

aceofspades
aceofspades

Reputation: 7586

Check out the annotate_models plugin on github. It will insert a commented schema for each model in a comment block. It can be installed to run when migrate is.

Upvotes: 4

Related Questions