How does ruby on rails work? - ruby-on-rails

I'm a PHP developer who knows a little bit of Ruby. I want to learn Ruby on Rails, but most of the resources I've come across treat RoR functionality as "magic" -- i.e., it has a certain internal consistency, but don't bother asking how it works in terms of Ruby, MySQL, etc.
Anyway, I want a deep understanding of how RoR works, the design decisions that went into building it, etc. In particular I'm interested in ActiveRecord, but really I'm looking for the whole package.
Any books / sites / advice welcome.

The books "Agile Web Development with Rails" and "The Rails Way" are both pretty good. "Pro Active Record" goes really in depth for Active Record, but doesn't touch on too much else. The podcast Railscasts sometimes just uses the magic, but sometimes it explains what is really going on. Various blogs such as Art of Mission can get into what your looking for.
Additionally, using the ruby-debug gem gives you a much better understanding of what is going on - you can step into what is running behind the scenes.

There are two areas here that can be examined separately:
General concepts (which include design patterns, general attitude principles like DRY and CoC or even agility etc.) since they are the principles behind many design decisions in Rails. Any of these concepts can be studied independently of Rails (since they are general programming concepts). It's good to have at least some understanding of these before digging any deeper. There are many sources about such general principles all over the net (aforementioned Martin Fowler's site is one of the better sources for grasping such concepts).
The way Rails implements these concepts into its corpus. There are, again, two things to have in mind here. Since Rails heavily exploits features of its mother language, it's crucial to understand Ruby's peculiarities (such as mixins and plenty of Ruby's dynamic features). The last thing to understand is how Rails uses these features, ie. how it's broken down into classes and modules, how many of its features are dynamically created on the run and so on. For this purpose, I highly recommend book Ruby for Rails from David A. Black (which is exactly about Ruby, its peculiarities and the way Rails uses them). While this book might be a bit out of date, I think it's still invaluable in its content.

If you really want to know how it works, you can just look at the source code. The online API docs let you see the source code of every method right in the documentation page.
ActiveRecord in Rails is based on Martin Fowler's Active Record pattern. It's basically an ORM (object-relational mapper). It's not even really that sophisticated as far as ORMs go. Java Persistence with Hibernate has a good overview of common ORM concepts.

This one might fit here well too -> http://railsforphp.com/

This is sort of a tangential answer to your question, but I, too, came from PHP development over to Ruby/Rails dev, and it was a big transition for me. As PHP devs, we're used to getting into the nitty-gritty, to to speak, and not being prevented from looking under the hood.
By design, Rails is a black box. You're supposed to learn Rails, almost as a language itself. It's a new way of thinking of web dev, especially from a PHP dev's perspective, though this idea is not uncommon to programming in general.
Not bothering to look under the hood may be the best way to start doing things "the Rails way."

Jamis Buck has a nice series of "Under the Hood" articles on his blog that cover some aspects of Rails' internals.

The "magic" in Rails involves method_missing and the concept of metaprogramming. Dave Thomas of The Pragmatic Programmers created a set of screencasts on metaprogramming Ruby.
PHP has a somewhat-equivalent function called __call, and PHP 5.3 introduced __callstatic. You might take a look at those, as well as search Google for "php metaprogramming", for some reading material on implementing Rails-like behavior in PHP.

I found this site a good starting reference:
http://www.tutorialspoint.com/ruby-on-rails-2.1/index.htm
It assumes MySQL in its examples.
I am sure you can find heaps and heaps by just googling for "ruby on rails tutorials", though.

ruby metaprogramming magic in rails: look at books by Rappin "Professional Rails" and Ediger "Advanced Rails", in addition to Black "Ruby for Rails" mentioned above
Naming conventions are important:
http://rails.wincent.com/wiki/Rails_conventions
http://www.softiesonrails.com/2007/10/18/ruby-101-naming-conventions

If you really want to understand Rails, read the source for the bit of Rails that you want.
/Library/Ruby/Gems/1.8/gems/ on OSX or freeze rails and have access to it in your RAILS_ROOT/vendor/ directory

Related

Ruby on Rails commands and conventions - what are they?

I have been making websites in PHP and MySQL for almost ten years now but I have never used a framework. This means I have hand coded everything and I know how the code all works and interacts with itself.
Wanting to expand my horizons I have picked up Ruby on Rails to learn another web language, framework, DB etc. I have been following the Ruby on Rails tutorial and it is going smoothly so far but what bugs me how much of a black box it feels. there's too much magic, and stuff happens just because it does. Example of this "magic" include, if I add to the routes file "resources :users" all of a sudden I have near infinite possible links like /new /user/1 /user/1/edit etc. Or if I want to add a column to my db table I have to do something like this in the console "rails integrate _to_table value:type" and then I have to "rake" the db.
I have been able to make stuff following the tutorial but I don't understand what I am doing. I know part of it will come through experience but I want to know how and why Rails is doing what it does.
What are some good resources, online and books, where I can learn how RoR works?
Yes, it takes a while to know what all the magic is, but you'll get there eventually if you stick with it.
The 'bible' for ruby on rails development is http://pragprog.com/book/rails4/agile-web-development-with-rails
The 'bible' for the ruby language itself is the 'pickaxe' book, with contributions from the ruby language author himself. http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177
Ryan Bates has done HUNDREDS of free sceencasts and he is famous for having a really great approach, using the framework effectively. Every good rubiest rate these highly. http://railscasts.com/
Many folks find the "zombies" courses really good. http://railsforzombies.org/
Finally I'll offer my own bookmarks site with over 50 sites for rails:
http://www.rormd.com/linker/groups/1
and 20+ sites for ruby at
http://www.rormd.com/linker/groups/4
While you are learning a good IDE can help a lot. I used eclipse, then netbeans then rubyMine (from our friend at IntelliJ, well known for their java editor. rubyMine has the most features. It is not free but for the price (somewhere in the $24-$75 range, depending on special offers) it's well worth the cost.
This is for newcomers if they wants to learn ruby on rails, even don't know Ruby. I can say try this first and jump on others book.
Just hit on below link and see there are two tutorials available for Ruby on Rails 3.0 and 3.2
Ruby on Rails Tutorials link for Rails 3.0 and 3.2
for those tutorials you do not required Ruby backgrounds
even I like Head First Rails, this book is also good for understand - just rails (no TDB), so initial you can set in rails environment and after that jump on above tutorial link
You can see the entire code on Github http://github.com/rails/rails.
The Jose Valim Book Crafting rails application help to understand a lot of internal in rails too
I would say that using the terms "black-box" and "magic" is a quite inadequate and maybe even a bit depreciative. I believe that the difference you are feeling comes from the fact that Ruby is a very different language than PHP, and that it is easier to code high-level abstractions and conventions in Ruby that in PHP. Rails is full of these abstractions and conventions, and these may be quite confusing, specially if you have no ideia of how they internaly work.
Maybe it's not about Rails that you should be reading. I'd say that you should try to understand Ruby in the first place. A good understanding of its blocks, its object model, and its mixins is mandatory in order make that "black-box" feeling go away.
Programming in a modern programming language ain't black magic. Debugging Fortran code by printing the code and using crystals over the papersheets to find the bugs was.

Rails learn's confusion

This is a beginner's rails learning confusion. When I learn rails, from time to time, I feel frustrated on rails' principle "Convention over Configuration". Rails uses heavily on conventions. A lot of them are just naming conventions. If I forget a convention, I will either use the wrong naming and get unexpected result or get things magically done but don't understand how. Sometimes, I think of configuration. At least configuration lists everything clearly and nothing is in fog. In rails, there seems a hidden, dark contract between you and the machine. If you follow the contract, you communicate well. But a beginner usually forgets items listed on the contract and this usually leads to confusion.
That's why when I first pick up rails, I feel like it is somehow difficult to learn. Besides, there are many other things that could be new to a learner, such as using git, using plugins from community, using RESTful routing style, using RSpec. All these are new and come together in learning ruby and rails. This definitely adds up difficulties for a beginner.
In contrast, if you learn php, it wouldn't be that bad. You can forget many things and focus on learning php itself. You don't need to learn database handling if you know SQL already(in rails, you need to learn a whole new concept migration), you don't have to learn a new decent unit test(in rails, usually they teach RSpec along the way because rails is agile and you should learn test-driven development in the early learning stage), you don't have to learn a new version control(in rails, you will be taught about git anyway), you don't have to use complicated plugins(in rails, they usually use third-party plugins in textbook examples! what the hell? why not teach how to do a simplified similar thing in rails?), you don't have to worry RESTful style.
All in all, when I learn php, I learn it quick and soon I start to write things myself. Learning php is similar to learning C/java. It tastes like those traditional languages. When I learn rails, it is more difficult. And I need to learn ruby as well (I believe many of you learn ruby just because of rails).
Does anyone have the similar feeling as I have? How do you overcome it and start to master rails? Hints will be welcomed. Thank you.
You do need to learn Ruby. Ruby is a language. It has its own quirks. You don't need to learn a lot of Ruby, but you can't learn no ruby and expect to write anything decent.
Next, you do need to learn the Rails conventions. Learning the conventions means that you don't need to learn the Ruby networking classes. If you don't want to learn the conventions, then writing anything of value will be difficult.
It's really no different than learning C# (as a language), then learning an MVC framework, like ASP.NET MVC, or FubuMVC.
Yes, you throw up a anything in PHP, learning a lot less in the process. But investment is valuable. Your education is valuable.
Learning takes time. Modelling is important, and I've seen some ugly Rails code because the authors were poor modellers. Those poor models turned into ugly databases. But it still worked! It met that customer's needs.
You comparision Rails with PHP does not hold, since Rails is a framework and PHP is a language. You should compare Ruby with PHP, or Rails with some PHP framework (I am not familiar with PHP frameworks and do not know an example)
But you are right about the things that 'magically' happen with Rails. I too had problems understanding them and looked many things in the Rails code to see what was actually happening and why.
To learn Rails you should first learn the language on which it is build (like you should do with any framework IMHO), so learn Ruby first. Personally I liked Mr. Neighborly's Humble Little Ruby Book a lot. It is written in a easy and nice way and is not boring/serious like a lot of books on languages.
After that I read/did all tutorials/guides on http://guides.rubyonrails.org/ they covert the (important!) basics and tell about the Rails specific things you are writing about.
When I first heard of Rails the claim was that you could build a web app ten times faster than the other frameworks. I always thought this was an exaggeration.
Most of the conventions are hard to remember if you don't have ten years of doing it the 'configuration' way. So, for someone new to web development it is hard to remember all the conventions and to understand why they are important. I definitely did not see a ten time increase in productivity. Routing, for example, probably started nice and simple but grew more and more complex. I think some effort should be made (perhaps by the people writing Rails books) to not try and throw every capability out there to confuse newbies.

The last 20% in Ruby on Rails

I am (quite) an experienced programmer but totally new to Ruby and Ruby on Rails.
RoR looks great to get working quickly, specially the automatic screen generation for CRUD operations.
It really it gets you productive quickly.
The questions is with the last 20% of the work, when I must finish my application. Won't RoR conventions stand in my way? Because not every database table must be available for all users, and not all users can edit all columns and/or all rows, and the views must be adapted to my site's look and feel, etc.
I understand RoR has been used successfully in live sites, but how exactly do you gain enough speed in RoR to escape gravity after the first phase has been burned out.
I don't think scaffolding gets you 80% there. Scaffolding is nice in that it shows you how the pieces of Rails fit together, but I wouldn't build my application off of scaffolding code. Now that you've been impressed by scaffolding it's best that you forget all about it. :)
Where Rails really shines in my mind is database migrations, the awesomeness of how dynamic ActiveRecord is, and the plugin ecosystem.
There's a lot to learn when deciding to go with Rails. You have a new language, new framework, and new plugins - but if you take the time to learn those things you can be very productive with Rails.
I've been doing ruby on rails for quite sometime. The 80/20 problem is not unique for rails. It applies generally to the entire world. I'm also not aware of any framework that can just do business logic for you.
To answer your specific questions. Conventions will not stand in your way while doing the 20%. Instead, conventions will help you to get through that 20% quicker.
Personally, for user authentication, I use Authlogic. For user authorization, I use Lockdown or Authorization plugin depending on customer need.
I also use inherited_resource in most of my projects to simplify controller code. This is another power of convention.
To increase speed of development, you will not only need to know Rails, but rails gems/plugins that does the right things for you, so you don't have to reinvent the wheels again. Also, knowing Ruby language is a must for quickly develop beyond the 80%.
Ruby Toolbox provides some of the most popular gems and plugins used in typical rails projects targeted at specific domains. You can look through the relevant categories and know what most people use. (And it's probably a good idea to use popular, well maintained gems)
TDD/BDD style development will also help you to speed up in the long run.
Lastly, a warning: If you stray away from rails convention, you will have a painful time in general.
P.S. I used Merb before. My feeling is that conventions helps you in merb, but you won't get too much penalty for not following them in merb. However, my experience with Rails is that if you decide not to follow rails convention while developing rails app, it will come back to bite you in one way or another! So think twice when you really attempted to stray away from rails conventions... (This is from my own experience, and of course subjective, but you can think of it as a warning...)
Won't RoR conventions stand in my way?
Because not every database table must
be available for all users, and not
all users can edit all columns and/or
all rows, and the views must be
adapted to my site's look and feel,
etc.
This is a bit of a non-sequitur. Rails is a framework that has been lifted from real world applications. Those applications had to deal with all those issues also, as well as others you may not have thought of yet. Generally, the conventions make life easier once you learn them.
Another point is that the conventions are merely conventions. You don't have to follow them. You do not even have to use RoR for everything, though I've yet to find a case where I didn't/couldn't, I do generally try to push as much into the DB or cache layers as possible.
I don't believe that you'll ever have a serious problem with Rails conventions. Just stick with the conventions and trust the RoR system. The people behind Rails put a lot of effort into these conventions to support 99% of the common usage scenarios.
If you really need to do something outside the conventions it will eventually get complicated quite fast. However, you are not alone. There are a lots of excellent resources on the net to get help (for example the StackOverflow community).
To sum it up:
Stick with the conventions whenever possible (excellent resource to get started: Rails Guides.
Don't reinvent the wheel. Look for Rails plugins and Ruby Gems instead (don't forget GitHub).
Consult the StackOverflow community if you need something out of the ordinary.
Test all the f***ing time (just for fun).

Learning Ruby on Rails with Pragmatic book

I only have a background knowledge of java and C, and a bit of unix (learned from the first year Comp Sci) and i do know bit about HTML/CSS... anyways so i've started using the book and the book said that we don't need to know html or ruby or css and that book would be kinda teaching all that as we go along..
And i'm at the part where we create a cart for the online store website, but i find that they just add a ton of html/css of ruby codes without really talking about it, and moving on.. I heard that this book was the best of there for ruby, i just focus on the stuff they mention, but i was wondering if i'm supposed to actually just look at other stuff when they add the CSS or HTML tags or ruby codes... i feel that this book is flying, without much explanation..
Should i just keep going with the book, or go back and study every line of all those codes they just put in the program?
I used this book in my first job and failed miserably. It was my first time and and didn't knew Ruby but was asked by my team leader to just go by this book. And I miserably failed to do anything other than the said methods in the book.
I suggest if you want to t have good grasp at web development, you need to know HTML, CSS and Javascript at a very decent level. After thatstudy Ruby. Then you can go through this book.
Now there was once an article online by a prominent fellow in the rails community saying something worse about this book. But since it isn't online anymore I assume that the blogger realised he was wrong and the book is indeed good. So just go about and find it for yourself. But as I said go through all the other necessary topics I mentioned before you jump into rails.
I've used the this book, is good but as you said they some things lack explanation. Usually the best thing you can do is pretty much use google or stackoverflow to lookup this things that you just don't understand. I was pretty much like you, 0 HTML or Rails, but after 2 weeks of reading code and looking for explanations on the web I can now pretty much understand all of the code, and afterwards everything is pretty straightforward.
My best recomendation is that you modify all of the code they give you, this will force you too lookup everything you don't understand. The good thing is that the code on the book is pretty good, and they try to teach you good programming practices on the way so stick to the book.
The HTML/CSS should be easy to pick up, but if you're having trouble following the Ruby code, I'd suggest you start with a Ruby off-Rails book. The Pickaxe book comes to mind. You can find the 1st edition online. Also the Ruby Way helped me to "get" the Ruby way of doing things.
I would continue with the book, it really is good. However, you might want to look at some other books as well including (but not limited to) Simply Rails 2 and Learning Rails.
I found the Pragmatic book to be a good start and the other two to complement it rather well. After that, just have some fun digging into some of the internals.
I would study the code thoroughly. I think you'll find that (if you keep working at it) ROR is pretty amazing.
I think your best bet will be to keep going at a steady pace on the book - going into more detail if you get lost, and if necessary grab a snapshot from online to keep pace if you break things.
From there I would start branching out into many of the other excellent resources available. Pragmatic Programmers have a book called Advanced Rails Recipes that I hear is also fairly good.
Since Rails is still moving rather quickly your best bet will likely be, once you have a handle on things, to start following some of the latest news. Ruby Alltop might be a good place to start looking for more resources. I would suggest going through as many Railscasts as you can - they are pretty much the definitive place to pickup Rails concepts quickly.
I would highly recommend supplementing the books with screencasts. Ryan Bates in particular has done an enormous amount of Rails screencasts at Railscasts, which are freely available. He has also done some paid screencasts for the Pragmatic Programmers site which are much more in-depth, and very helpful.
Creating multi-model forms was a bit of an early sticking point for me, and Ryan's Pragmatic Screencast series on forms was very helpful. I would also recommend the ActiveRecord series.
I started Rails when it was 1.3. I followed the second edition of Agile web development with Rails. I was facing hard time finding how the things were happening. As I did not have any experience of web development this was more painful. I followed the book and created the depot application twice but I still did not get very much from this book.
I was having trouble understanding the Ruby code mainly. As said by "listrophy" on #rubyonrails
Using rails without knowing ruby is
like trying to write a menu at a
chinese restaurant with only an
english-chinese dictionary.
You should get yourself up and running with Ruby first only then you should dig into Rails. I highly recommend you the Ruby for Rails book by David Black but for Ruby only. Read part 2 and part 3 only from this book. The things about Rails in this book is crap IMNSHO. When you are done with Ruby from this book, carry on your journey with the Agile rails book.
When you are done with all this, you can check out The Rails Way, The Ruby Way, Pro ActiveRecord books.... The list goes on and on... :)
The depot application in the Agile book is a great example of a sample Rails application. However I suggest you to check out Open Source Rails for some more sample apps. Rails Guides also have some very good tutorials on rails. As said by everyone else Railscasts by Ryan Bates has some very high quality free rails screencasts.
FYI I have a little knowledge about HTML and very little about CSS. You can not learn these technologies by following some book. You will learn these with the experience and time. Although some books can give you a good start. But I do recommend you to learn Javascript-Ajax and stuff. For this purpose you should use libraries like Prototype or Jquery. I recommend Jquery as this is much more easier to learn and also more powerful.
HTH :)

can users who have used both Django and Ruby on Rails give a little comparison of using them?

Duplicate: Django or Ruby-On-Rails?
I have been reading on Ruby on Rails, and it seems like on some threads, some users like Django a lot too?
Can someone who have used both give some insight about using them, such as
ease of use
productivity
fun factor
deployment issues
or any other framework you'd highly recommend?
Both are excellent frameworks. Though, I've found Rails to be more suited for the agile developer. For the most part, you'll run some generators to get the files you need as placeholders for your code. Things will work right away, and you just build up from these conventions. It's really flexible and has a large community, lots of innovation and interesting practices are being put into Rails. It's development cycle seems faster paced than Django.
After only touching the surface with Django, it has some interesting differences. As far as I know, you don't get the schema migrations like Rails has out of the box. But you get an extremely simple CRUD mechanism for your models with the extensible admin interface, which is great for testing/managing content. The entire project is documented really well, from the Django Book to the vast amount of information on docs.djangoproject.com.
I personally prefer the Rails way of doing things. But honestly, you need to try them both to see what works for you, and since we're talking about two very good, yet totally different frameworks, it's a tough decision to make either way. So, if you already know Ruby or Python well enough, start with what you know and just go from there. Once you understand how one works, you'll be able to evaluate the smaller differences yourself. Hope that helps.

Resources