What specific piece of advice could have saved you from going down the wrong track, saved you lots of rework or just helped you generally?
views:
656answers:
11Models don't need to extend ActiveRecord (represent a database table) - they can just be for some form data or a non-persisted class used by your controller.
Kept me from going down the wrong track: Namespaces in Rails aren't worth the hassle.
Saved lots of rework: You can write your own generators.
Just helped generally: rake db:fixtures:load
Bonus tip (not Rails specific): Learn version control. Even if you're coding alone.
Edit: Loading fixtures into the dev database still seems wrong. Suggestions are welcome.
The main one for the first days? Don't Panic. The rails api is larger than people really advertise. It will take a while for you to memorize the portions that you use on a daily basis. Parts of it won't make sense until you use it for a bit. This applies doubly so if you're coming from a c/pascal/algol style language and using a language like Ruby for the first time.
Here's a grab bag of other ones:
- Fixtures suck. Really. Don't use them. Instead do inline setup in your tests. There are plugins to help with this.
- Fat model, skinny controller.
- As a corallary to the above: Named scopes rock
- The official API docs blow for searching. These don't.
And then there's my standard list for development as a whole:
- You really do need version control. (preferably git at the moment)
- Keep it DRY
- When do I test? TATFT
- Tell, don't ask
- Do the simplest thing that could work
There's no silver bullet to hacking rails, or otherwise, but I believe considering the above will save most people enormous amounts of pain. It's a sad commentary on our profession that so many wander lost in the hinterlands attempting to wrangle piles of spaghetti code via their clipboard hoping that it'll all just work in time for the deadline.
Learn by actual information.
Rails changes very quickly and you will get a lot of troubles if you will use information related to the old Rails version while you have modern version on your computer. If you are reading post in blog take a look at it publish date. If you are reading book then read about Rails version it is uses in examples.
Learn Ruby First.
Rails is, unfortunately, not magic - most of the good stuff in the framework comes from the power and expressiveness of the Ruby language itself. You need to have a solid grounding in Ruby to get the most out of it, including Ruby metaprogramming techniques for when you (inevitably) have to dig into some of the Rails source code to understand/fix something.
I'd highly recommend the Manning book 'The Well Grounded Rubyist" for a good overview of the language (http://www.manning.com/black2/)
Take a good look at the Hash and Array classes and understand what they can do - so many of the data manipulations you'll need are really easy ... once you know how.
Make sure you've got a plan for how you are going to host your project. Good Rails hosts aren't as easy to find as PHP hosts.
http://stackoverflow.com/questions/251418/who-are-good-web-hosts-for-ruby-on-rails-projects
I've made several attempts at learning RoR. First (and unsuccessful) attempts I've looked from top, installing instant Rails, generating scaffolding, learning by reading. I had lots of experience programming in C++, Java, and C# and I thought that just some scripting language doesn't need to be learned.
And I was wrong. Everything really seemed like magic, I had no idea what the fuck was going on. Only after several attempts, separated by several years, I've finally started understanding Rails.
The way I did was not to look at a finished Rails scaffolding but start from basics and break stuff. I would start from the Ruby itself, not even thinking about Rails, breaking everything to understand how it works. Then I'd pull some gems, including ActiveRecord to learn how ORM works. Then I learned about views. I really broke everything apart and learned about it individually a long time before I finally typed in "rails test_project" and started understanding MVC and how all the components worked together.
What would I advice: break everything. In both meanings: break so it doesn't work (and understand why) and break into small parts to ease understanding.
Be sure you're already confident working with databases, (rails models will burn you if you don't have a good understanding of SQL)
Watch out for RJS: it's a ruby programming model for manipulating a page with AJAX. It may really help you, but if you're not also aware of pure javascript frameworks like jQuery you may find your productivity drops when you try to make highly dynamic sites.
There's a rails plugin for almost everything.
Most plugins are hosted on git. If you're using something else, (like SVN), get an IDE like RubyMine which will happily let you use git-hosted plugins while your project is hosted in a different version control system.