Tutorial teaching Ruby syntax/knowledge required to easily learn rails - ruby-on-rails

I am trying to learn ROR these days and have basic knowledge of ruby, but often working with rails, I get to the point where it seems as if I don't know a bit about ruby.
Just to explain the point, in rails we use has_many keyword. I did not learn any such thing when I was going through ruby tutorials but just came to know that it has something to do with meta-programming in ruby (I have no idea what is meta programming).
So I would like to know if there is any book/tutorial which explain all the points/syntax/concepts of ruby, which a newbie would see while programming in rails.
Thanks to "Jonas Elfström", in simple words, what I am looking for is to know "how Rails uses Ruby"
Thanks.

has_many isn't a keyword, it's simply a class method in the ActiveRecord::Associations module.
It's documented here and you can even view the source if you scroll down a bit.
Associations are a set of macro-like class methods for tying objects
together through foreign keys. They express relationships like
“Project has one Project Manager” or “Project belongs to a Portfolio”.
Each macro adds a number of methods to the class which are specialized
according to the collection or association symbol and the options
hash. It works much the same way as Ruby’s own attr* methods.
If you already know Ruby the Rails Guides could get you going but it might be easier to learn from one of the books listed at the documentation page or by watching a couple of screencasts.
For books about Ruby I've never seen such praise as what Eloquent Ruby gets.

There's a tutorial called Learn Ruby the Hard Way
(written by Rob Sobers of Fog Creek Software)

I tried lately this: http://pine.fm/LearnToProgram/ which helped me a lot because there are one or two concepts that had slipped. I started long ago with an arcane version of this.

Related

Ruby Learning - Know of any public repos with Ruby code of Intermediate difficulty?

I've been going through Ruby (and Ruby on Rails) tutorials online such as RubyMonk, Codeschool courses, the Railstutorial, and Learn Ruby the Hard Way.
These are all excellent resources, but I feel like I need to see some Ruby code that is actually being used in production in order to advance to the next level. I would assume that a repo like Rails would be considered advanced level - so I'm looking for something a little in-between.
Are there some GitHub public repos that are using a more intermediate difficulty level of Ruby coding that I can learn from?
If you can recommend some that would be an appropriate follow-up based on the contents of the tutorials I listed earlier, that would be great.
I've looked some myself, but I'm asking for recommendations because I want to make sure I'm studying something that is "the right way" to do Ruby and not learning bad habits.
Here's two I've created:
http://github.com/rails3book/ticketee - A light-weight project management system from the http://manning.com/katz'>Rails 3 in Action book.
http://github.com/radar/forem - A forum engine for Rails 3 applications.
Both should have pretty good examples of intermediate code, along with tests.
I published a solution to a ThoughtWorks interview problem that I used as the example for a talk. I'm not sure it's "intermediate", but quite a few things in there might fall outside the range of "beginner", so I think it might be up your alley. There's also a rather robust test suite using MiniTest::Spec and mocha.
For example, I use this approach to cause classes which include a module to register themselves with their base class:
module TaxWorks
module TaxStrategy
def self.included(base)
add_strategy base
end
def self.strategies
#strategies ||= []
end
private
def self.add_strategy(s)
strategies << s
end
end
end
In this example, now you can do TaxStrategy.strategies to enumerate the list of possible strategies, after loading any you care about.
Hope that helps!
If you're into Rails you should have a look at these open-source Rails apps:
https://github.com/rubygems/rubygems.org - Source code for rubygems.org, the Rubygems server/website
https://github.com/ryanb/railscasts - Source code for railscasts.com
Both of these apps are exceptionally well organized, clean and thought out. In the case of rubygems.org it's had tons of contributions from different rubyists and is a great working example of Rails in action.
I think Sinatra is a good start. Take a look at Sinatra's source code and build a small MVC application with some helpers. You will not only learn some ruby but also get a good understanding for how Rails work without the magic. It is made "the right way" and you will not learn any bad habits.

Advanced Ruby on Rails database?

Im taking on an upcoming project that involved creating and managine a large complex mysql database using RoR. It involved, many table, deep foreign keys, many to many, etc.
Anyone know a good resource (book, website, etc.) which can help me learn how to do it? I need a clearer understanding of migrations and how rails handles relational databases.
Any suggestions?
railscasts.com is great. He talks a lot about setting up your typical HABTM relations polymorphic associations, inner nestings you name it. and Bates makes it extra easy to understand.
Past that I would definately try to start off on the right food using the Metrics gem to monitor database and CPU usage. Always ensuring you're making the lightest touch on the db.
Besides RailsCast, I would recommend RailsLab: Scaling Rails which focus on large Rails application. It seems doesn't update recently, but the post still useful, not only database, but also memory, performance etc.
+1 for Railscasts.com suggestion from Trip. I've gotten a ton out of those since I moved to Ruby and Rails from ColdFusion. I also like the book "The Rails 3 Way" from Obie Fernandez. It has an extensive ActiveRecord section that is very informative both for new Rails users and is a great reference. Another solid one is "Rails 3 In Action" from Ryan BIgg and Yehuda Katz.
You can check out books at the Pragmatic Programmers website. Probably one of the best options is: Agile Web Development with Rails
I also found Ruby on Rails Tutorial to be a good resource.
Also be sure to look at the Rails Guides They are well written and cover many topics in depth. For example, the migrations you mentioned and many to many associations You can even download them for offline use. Here is an answer about how to do that here
Code School have some great interactive courses such as Rails for Zombies.

Multi Table Inheritance with rails 3

Are there standards or best practices yet when it comes to multi table inheritance in rails 3? So far the best article I could find was:
http://mediumexposure.com/multiple-table-inheritance-active-record/
But even that needed some changes(e.g. moving the requires to an initializer instead of the old /config/environment.rb)
Any better resources / standards?
For an easy way to do multi-table inheritance take a look at 'acts_as_relation' plugin https://github.com/hzamani/acts_as_relation.
I recently forked a promising gem to implement multiple table inheritance and class inheritance in Rails. I have spent a few days subjecting it to rapid development, fixes, commenting and documentation and have re-released it as CITIER (Class Inheritance and Table Inheritance Embeddings for Rails).
Consider giving it a look: https://github.com/PeterHamilton/citier
It actually takes some concepts from that article you mention.
There's a guy in the Melbourne Ruby group I attend that's written a couple of blogs on table inheritance in rails and the comments are really helpful as well. It's not specifically Rails 3 but there's definitely some decent pointers in there.
http://rhnh.net/2010/08/15/class-table-inheritance-and-eager-loading
http://rhnh.net/2010/07/02/3-reasons-why-you-should-not-use-single-table-inheritance
happy reading

How to use Rails or Ruby documentation?

I'm coming from C#, and recently I started to write some Ruby on Rails applications.
My biggest problem with it is the documentation because I find it extremly difficult to use. For example, finding out how to call generator from my controller took me about 2 hours and then 15 minutes after that I found Rails::Generators.invoke method to figure out what arguments should I pass to this function.
Maybe I use the documentation the wrong way; First I take a wild guess and search in the Netbeans code completion, which is rarely helpful, then I search in Google, then go with the API.
Can some experienced Rails programmer give me some advice?
If you're new to rails I recommend you read a book before you dive into the API documentation.
Here are two recommendations:
Agile Web Development With Rails - Great introduction and reference on how to use rails.
Programming Ruby 1.9: The Pragmatic Programmers' Guide - More on Ruby. Great references!
After you've worked your way through those books, you'll have an idea of how Rails is designed and where to look for stuff.
Both books are great reference books. If you want to access the Rails and Ruby API's online, make sure to checkout RailsApi.com
I am describing my own very personal workflow with Ruby/Rails documentation:
You can use ri and rdoc if you are familiar with the console, but...
Personally, I recommend two very good Ruby/Rails documentation sites:
apidock.com (no good search, but sometimes fine examples)
railsapi.com (very good AJAX search, plus you can customize what documentation you would like to see - by versions and by Gems)
Every iteration of the Rails guides (http://guides.rubyonrails.org/) has a ton of useful stuff in it. That should be the first stop.
After that I search away in the API - http://api.rubyonrails.org.
Sometimes I even end up just reading the source code trying to find things - https://github.com/rails/rails.
I think the best place in the source code to be reading is in here - https://github.com/rails/rails/tree/master/railties/lib/rails.
I upvoted each of the other answers here.

How to document a rails application?

I just started to document a rails application. I know this is actually done by rdoc, so I followed some rdoc guides regarding syntax and so on, but I got stuck when I tried to describe attributes of models, validations and the relationship between models, mostly because these things are part of ActiveRecord. So I wonder if there is some guide or a good practice regarding how to document a rails application or if there is something I'm missing?
I know that I could put all of this in the class description, but I wonder if there is a way more closely tied to the declaration itself (has_many, validates_presence_of, etc.) and what about the attributes?
I personally prefer YARD - http://yardoc.org , as it does a better job in documenting IMHO.
I don't know if there's a specific handler for Rails available, but it's quite easy to write one - http://yardoc.org/guides/extending-yard/writing-handlers.html
A good example might be the attribute handler - part of the yard gem:
lib/yard/handlers/ruby/attribute_handler.rb
Remember your tests are part of the documentation (for developers), particularly if you are using Cucumber where scenarios are easy to read. If you keep your methods very short and there is a test method with a descriptive name e.g. "should set the users name" I find I typically don't need comments on the method.
Validations or other parts of Rails I would not document. Part of being a Rails developer is understanding how these work, I think it is a fair assumption that another maintainer of your code reading it down the road will know validations, or other things built in to Rails. By that same logic, if you can use features of the framework or happy paths (not deviate much) with [documented] third party code, a lot of the documentation will be written for you.

Resources