GeeCON Prague 2014*
The moose likes Ruby and the fly likes Do models in Rails bother you? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Languages » Ruby
Bookmark "Do models in Rails bother you?" Watch "Do models in Rails bother you?" New topic
Author

Do models in Rails bother you?

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Hey,
Considering this Rails model:

Does it bother you that models in Rails don't include the fields that made the entity? it is implicit thing.
You have to check the data base tables schema every time you want to check model's fields.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No, it doesn't bother me. One of the main goals of RoR was to not repeat yourself. You already have the data model defined elsewhere; why define it again? It's in the migrations already, and via migrations, in the DB. There are plugins that will add comments to the AR class showing the fields as well.

There are any number of ways to get the fields without going to the DB, too; this is Ruby we're talking about.
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

But it is easier and faster to have one look on model file to figure out what it is made of (at least to me).
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I was just coming to this forum to ask about this. Right now, it bothers me. Probably because I'm used to seeing them in the model. Jumping around between files to remember what the properties seems a huge PITA for me. David, do you have links to the plugin you mentioned? Also, when you update a migrate script with new columns, does the plugin update the comments in the model?


GenRocket - Experts at Building Test Data
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Annotate models plugin, I think? I don't really remember; it was some time ago.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

http://github.com/ctran/annotate_models
http://snippets.dzone.com/posts/show/5456
http://exceptionz.wordpress.com/2008/03/18/textmate-command-to-annotate-your-current-activerecord-model-with-the-db-schema/
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

It used to bother me when I was only used to editors like Eclipse and NetBeans.

If you're using Vim or Emacs, it's not too hard to set up a shortcut to jump to the appropriate area of your db/schema.rb file. For instance, using rails.vim (https://wincent.com/wiki/rails.vim_cheatsheet), I can be in the user.rb model and type :R Enter to jump to the users definition in the db/schema.rb and then hit g Enter to return to where I was in the user.rb model.

Having this in my tool belt I think it's significantly *better* to not cloud the models with attributes.


A good workman is known by his tools.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(Personally, I agree; I enjoyed having the definition of DB stuff in only one place (migrations, usually) and used other means to navigate. I didn't mind having the docs, but I rarely did that either--mostly when it was other people's model.)
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

It's still really bothering me. Of course, this is my first day really digging into Rails in over 2 years. And at that time, I didn't even get this far. Just something else to get used to I suppose.
Mihai Lihatchi
Ranch Hand

Joined: Oct 28, 2005
Posts: 138

Time for my 2 cents .
It does bother me but if you have a look at the code in the sample Depot Application for the models (generated by Netbeans) you can put nice annotated docs to suggest the fields (I believe the book "Agile Development with Rails" specifies more).
I got used to having the DB console open in the first stages of developing the application. Of course with Java/Hibernate things are not so bad but it's not a major block.
The fact that you can discover the error in an instant kind of compensates for this small annoyance.
This reminds me I should use that plugin for my current RoR code .


Better, faster, lighter Java ... you mean Ruby right ?
SCEA5,SCBCD1.3,SCWCD5,SCJP1.4 - memories from my youth.
Jeff Boucher
Greenhorn

Joined: Aug 18, 2008
Posts: 14
I'm coming late to this discussion, but this has been on my mind lately as well...

I don't feel completely comfortable with Models in ActiveRecord either, for the same reasons mentioned by a few other posters above. A lot of this boils down to personal preferences and style - for me, the Model object is the foundation of how I think about code. I often think about a problem in terms of objects (which I'll admit isn't always the best approach), and when I start writing code, I often begin with the object model. ActiveRecord clearly takes a different approach that has proven to be immensely productive. However, I'd rather be able to define my objects more explicitly.

Do keep in mind that this is really about ActiveRecord, not rails per se. There's nothing really stopping you from writing your own object model and persistence tier in Rails if you want to do things differently. Another thing you might want to do is look into DataMapper, which is an alternative to ActiveRecord that would probably appeal to people who want more direct control over the model (check out datamapper.org).

That said, I haven't really used datamapper, mainly because in spite of what I wrote above, I'm willing to stick with ActiveRecord. I think I gain much more than I give up, and if I'm using Rails, it's probably because the rails conventions line up very nicely with my project. Keep in mind that a lot of extremely useful plugins also assume ActiveRecord. One of the things I like most about Rails is how easily everything seems to work with minimal configuration - in other words, I'm often willing to make a trade off to take advantage of Rails conventions, including ActiveRecord.

But yeah, for those of us who tend to think in objects (POJOs, for instance), it can be disconcerting to open up a model and see nothing there.
 
GeeCON Prague 2014
 
subject: Do models in Rails bother you?