简体   繁体   中英

How does ruby on rails work?

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" -- ie, 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:

  1. 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).

  2. 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.

这个也适合这里 - > 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在他的博客上有一系列很好的“ Under the Hood ”文章,涵盖了Rails内部的一些方面。

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM