开发者

Creating readable models in rails

开发者 https://www.devze.com 2023-01-28 06:02 出处:网络
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 mode

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?


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.


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.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消