Hey, this is something every ruby guy should know. If you're a contract worker you'll walk into a project sometimes and you'll find a management team that wants something more effective, and a staff that's stuck on what they know, just because they already know it. While people have a valid point that learning something new is hard, Ruby on Rails provides so many benefits to the development of a project that the learning curve is an acceptable loss of time. These are the things that stopped my dead in my tracks about ruby on rails development.
- MVC - If you're a PHP guy sitting there going "What does 'MVC' mean?" i don't blame you. The fact of the matter is, PHP is a fucking mess. Even highly touted web frameworks in PHP aren't really MVC based. MVC means Model-view-controller - and really it's the best way to construct a web application and keep the views separated from the logic code. Models in Ruby on Rails typically represent tables in the database, and lets you extend special properties to the objects if you want, while controllers use these model objects and organize them to put into the Views, which are the templetes.
- ActiveRecord - if you know a PHP guy and you want to convert them into a rails evangelist - show them ActiveRecord!! Since ruby is so object-based, ActiveRecord objects are directly related to the tables in the database. This means that you can completely avoid writing sql. Now, if you're sitting there thinking "But I already know how to write SQL, why would I do that?" The answer is simple. What if you develop a site using MySQL and find out that your client's host is on Postgre? Well, in PHP you're fucked, and in ruby you have to change like one word, and everything works. Also, consider your site gets really popular and you find you'd like to switch to a different database for scalability reasons. Once again, on PHP you're fucked, and in Rails it's done for you. The real power here is being able to treat your DB tables like real objects. This means that if you have a table with geographic locations in latitude and longitude, you can easily extend that table's class with a 'country' method which returns what country the lat/longitute are in, and calling it is as simple as
GeoCodes.find(id).country Which will find the object with the proper ID in the database and return the result of the find_country method. The php equivalent to this is MUCH more messy. Also, everything about the DB layer is handled by rails. This means that really complicated things are handled for you. What if you have two tables that are related? The type of tables you would do an INNER JOIN on in sql. In rails, if you've got one object and it's related to more objects in another table, getting to these other objects is as simple as a method call. Lets say that you have a people table in your DB and each person has many cakes, which are referenced from the cakes table. So, you need to get the person object:
person = Person.find id Easy, right? Now you want to get their cakes:
person.cakes Wow! ONE word?? Yes indeed. Now lets say you only want the cakes from this person with chocolate on them:
person.cakes.select{|cake| cake.icing == "chocolate"} How about only chocolate cakes with less than 4 candles?
person.cakes.select{|c| c.icing == "chololate" and c.candles < 4} ...I believe I've made my point. In php (to say the least) ll this kind of stuff is painful and shitty to write, either because you're writing it in pure SQL and using PHP string operations to modify that sql, OR because you're doing it all on the PHP side with loops. Either way, it's messy and it's not as clean as this.
- Complete Ajax Integration - Haven't you ever wished you could write client-side JS code from the server side, so you could consolidate all your logic code into one place? Well, I've often wished this using PHP, and what it comes down to is that you have to include JS into your templates and then carefully try to use the server output and the templates together to make some hoppin' AJAX goodies. You can also output ajax from php using strings and string manipulation techniques. Hooray.. In Rails, all of this is fully integrated. For example. You want to make an ajax call in rails that updates an element (let's say a news ticker) with a new random entry:
def update_ticker news_array = NewsItems.find :all rand_news = news_array[rand(news_array.length) ] render :update do |page| page['id_update'].replace_html rand_news.body end end which will actually output the necessary cross-browser compatible javascript code to replace the html in the div with id="id_update" with the body of the new random news item. All this without ever actually writing javascript. Pretty cool huh?
- Convention over Configuration - This is important. Rails always assumes that whatever you're doing, you're trying to do it in the most common way that it's usually done. This means that it will assume that you're doing it in that common way, but if you'd like to change something about that (i.e. configuration) then you're free to do so. In PHP this is *not* the case. In php you're generally forced to point out every stupid detail of something before you use it, even if you're using it in the most common way it could be used. Examples? Lets say you have an action in your controller named hello_world. Rails will automatically look for your template for this action in views/controller_name/hello_world.rhtml, meaning that you don't have to write ANY code for it to understand that this is where that template is located, and try to use it when someone navigates to "yoursite.com/controller_name/hello_world". However if you'd like to use a different template for your action you could always call
render "controller_name/different_template" ...or what if you want to render the hello_world template, but you want to render it in a different page layout than your site's default layout? No problem:
render "controller_name/hello_world", :layout=>"different_layout" Basically everything in rails acts like this: by default everything just works in the most common way possible, and if you'd like to add to it, you're free to. In php, nothing works until you make it work.
- Plugins - Since rails is so well structured it's a very friendly framework to write plugins for. Say you've got users, and your users need to upload image attachments. Well, in php you have to write the image uploader, then you have to write the code that takes it from the tmp folder and stores it somewhere userful, and renames it, and checks that it's the right filetype, etc. In other words, in PHP there's 1,000,000 things you have to be aware of and configure so that your users can upload images. In Rails it's as simple as
script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu/
After which you just specify that Users have attachments associated with them by putting this in the User.rb class:
has_attachment :content_type => :image, :storage => :file_system, :max_size => 500.kilobytes, :resize_to => '320x200>', :thumbnails => { :thumb => '100x100>' }
WOAH! look at that! We just killed like 200 birds with one big line! You specify the max size (note also how sexy '.kilobytes' is), and even what size to resize the image to (and built-in thumbnail support!), just right there! If this could be easier, I'm not sure how. All of your image upload needs are basically taken care of for you by a 3rd party plugin for the framework, and it's fully customizable. You could attribute associated image information with any of your existing models (DB tables) in your app, in just about one line.
Alright.. So there are really a WHOLE lot more items i could go off about, like blocks and passing blocks, gems and the incredible power they provide, database migrations, or the built-in testing framework, or the power of clean and easy URLs with routes.rb, but I think the really core differences are the ones up there. Anyway, have a nice day! If you know any die-hard PHP people, feel free to make the web coding world a better place for everyone and show them this list!!
|
I hate to break this to you, but you are comparing a Programming Language(PHP) with a Programming Framework(Rails). Next time you write an article bashing a particular technology with excessive use of offensive words, educate yourself first.
I have to say, that ORM, and in ROR case, Activerecord, is exactly one reason NOT to do the switch.
Let take your arguments:
1) "avoid writing sql"
Not really an argument on the plus side to me...
I'm more a DBA than a web developer, and I tend to implement most of the core functionality in stored procedures.
In that case, ORM mapping are useless.
And for optimization, I prefer crafting my own queries, than relying on a auto-generated one, that would maybe not be up to par.
What I never understood too is how to represent outer joins with any ORM, and as I have understood, it implies several ORM queries intricated each into the other.
2)"What if you develop a site using MySQL and find out that your client's host is on Postgres"
Then it sucks to be you !
If you don't know the basic requirements of what your job is, excuse me, but you've done something terribly wrong at the beginning already.
I don't see what is so great about DB portability.
I worked on so many projects, size small to medium, and DB portability NEVER has been anything that was planned, nor done.
Each databases has it's specifics strong points, and fully configuring 1 is not a 10 minutes job.
If you suddenly see that you must change your DB back end, then again, something has been really bad thought at the beginning....
3) "consider your site gets really popular and you find you'd like to switch to a different database for scalability reasons. Once again, on PHP you're fucked"
Not if you started it right.
If you have wrapped the calls, then it's nothing to change it. Except some queries that might need to be rewrote, but nothing more.
4) "Haven't you ever wished you could write client-side JS code from the server side"
Not really no. I love javascript. I find it really fun to play with, and once you know how to manipulate the DOM, there is not more problem about portability.
And again, not much problem about the interactions if you start it right.
I do the basic to work ok without javascript. Then, I add a custom namespace to my HTML code, and the javascript behaviour I add are hooking themselves on attributes in that namespace.
For example, I add an js:ajax="fx_name" on a form element. If no js is there, the form will submit like before.
If js is enabled, the script will replace the with an with the specified function as handler.
Simple and clean.
I admit that I never tried ROR.
Mostly because of "Convention over Configuration". I have found myself often not going on the conventional road, and I don't want to spend my time fighting against a framework.
Although it's what is happening in the last weeks, as I started migrating an PHP web site (which took 10 days to develop, without any frameworks) to a django python project.
So far, it's going relatively good, but I must admit that I'm swimming against django on many points. Particularly on the MVC, as I don't want to use any forms of ORM, so no models neither...
I like python as a language, as much as I like PHP as a language.
But as I never found any framework that suited me in PHP, nor in javascript and I still haven't found anything that really suits me in Python.
Ok, I thought I'd respond to your points also:
1. Agavi (my preference), Symfony, CakePHP, Zend Framework, CodeIgniter, Seagull, Solar, etc.
Personally I use Agavi, and honestly it kicks the living crap out of anything else on the market for the top three dynamic languages (php/python/ruby).
2. phpDoctrine, Propel, Zend_Db
phpDoctrine does more then AR does in rails, and faster, and with an object query language that is really neat.
3. Again, depending on 1. almost every php framework supplies this. The thing is though that sure, the small quick things can be done automatically by the framework - but the advanced ajax functionallity almost always has to be handcrafted anyway so the gain from this is really really small.
4. This is only a matter of preference, if you like CoC or plain Configuration. And again it depends on the framework you pick.
5. Is there a framework that doesn't supply you with the ability to extend it?
As we've already said, Language vs. Framework, that's like comparaing an car engine with an entire car.
Now stop pesterising the interwebz with your clueless posts... thank you.
dude(s), i compare a framework here with a language for a load of reasons. Ruby is a language used for a great many things, including standalone applications, telephony, and a whole basket of other goodies that have nothing to do with web dev. PHP does web dev, period. So, that being the case, i wanted to compare the web development arm of ruby (rails) with the equivalent "subset" in php, which really is the whole language. Also, maybe i would have picked out the most popular framework in php, but really all the php frameworks are largely the same: a mess of new functions to remember, and poorly organized and integrated functionality. In ruby, the web framework (rails) integrates with the language via 'monkey patching', which in a way makes the rails libraries a very convincingly integrated part of ruby itself, unlike anything I've ever seen in PHP. Anyway, I didn't work on this article half enough but published it anyway - will stop doing that in the future..
If you're stuck on agavi you probably don't have much of a clue about efficient programming. I bet you $50 that i can write anything you can write in ANY php framework better in 1/3 the time in rails, and with 1/3 the lines of code. That's kind of the point I was trying to make, cleaner and shorter code makes happier programmers. I realize also that AR is available for php, and also that there are AR alternatives, however in languages that aren't ruby (maybe with the exception for python) the benefits you reap from using AR are pretty minimal. Things just plain don't integrate well with php. No matter which framework you use you end up with a mess, where each version is basically a fork and things have to be re-written for existing components to keep working.
anyway, thanks for being such an elitist fred, people like you make the programming community a crappy place to be.
Sorry, but PHP is considered a framework, not only a programming language, for a number of reasons.
Google it and you will find why.
Very nice article!
yztxtwms http://mcvnkdor.com snjudswh agandrcl ppovecxu [URL=http://zkwhtjva.com]dyptrdzl[/URL]