File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do models in Rails bother you?

 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3479
Clojure Mac Objective C
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Annotate models plugin, I think? I don't really remember; it was some time ago.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Jeff Boucher
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic