• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Do models in Rails bother you?

 
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Annotate models plugin, I think? I don't really remember; it was some time ago.
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 138
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic