Models in Rails are implicit, is it really annoying?

Consideration of models in Rails:

class Organization < ActiveRecord::Base belongs_to :OrgType end 

Are you worried that the models in Rails do not include the fields that made the object? I know this is done for DRY, but you need to check the database table schema every time you want to check the model fields.

+4
source share
4 answers

If you prefer the declarative style of the ORM model, you can check out DataMapper or Sequel , both of which can easily connect to Rails 3.

+3
source

Not annoying me ... since I started using the annotate model drawing to automatically add comments to the top of my model files that list the fields associated with this model.

+3
source

I listened to the podcast a few months ago when the guy who hosted the cast and the guest advocated that Rails should go this route and end migrations. The guy repeated that β€œmigrations must die,” suggesting a way to point your circuit to the model.

I can no longer disagree, and I hope that Rails will never take this path. This is not only DRY, but I like the fact that Rails encourages you to know about your own database schema and structure.

In addition, it would not be possible to preserve the history of your circuit if models controlled it without excessive dullness. Migrations are essentially version control for your database, which is developing with your application ... and I would not want that.

+1
source

Suppose OrgType has a field called position . This is often the case when you want to present a selection list to users who will select a type. It is very unlikely that your Organization will always take care of this field. Now extrapolate this to other related models with fields that other models do not like, and add in the fact that if you ever want to change one of these fields, you will have to look for every declaration, not just every one where they are used.

This is a mess.

Open your db tool and look at your fields when you want to know what they are. Keep your models clean and readable. If you see a code like this:

organization.org_type.name

It is pretty obvious that OrgType has a name field, without having to look for it, and without having to go through the configuration mess in each model.

0
source

All Articles