Ask HN: Ruby on Rails why the hype?
Most of my recent coding has been done in asp.net and asp.net mvc of late. I was looking into Ruby on Rails to figure out what all the hype is about. What I can't figure out is where is the business tier? All of the code samples in the "models" that I've seen seem to be straight to database type code. I’ve seen lots of pretty and fast sites using RoR but they don’t seem to do anything computationally interesting (in process). So what I’m I not understanding here?
28 comments
[ 3.4 ms ] story [ 23.6 ms ] threadA better one is to build a gem with c bindings (RMagick has bindings for all the ImageMagick functions right away).
Alternatively, you can wrap into a gem with a C extension and call it with all the Ruby goodness you should expect (recommended route).
Pseudo-code (wordier than it need be):
class MyModel
endA good example of a gem with a C extension might be RMagick (http://github.com/rmagick/rmagick/tree/master/ext/RMagick/)
RubyInline is incredibly powerful. You can drop C straight in to your Ruby scripts for operations that would be too computationally intensive for Ruby.
Ruby C extensions are more powerful, as they allow you to call C code from within Ruby. However, it does require some alteration to the C code, so this won't do if you have a closed source C library.
As others have suggested, there are ways to make system calls from within Ruby, and with the way things work on Unix systems, that's a perfectly acceptable alternative.
Also, to address your question more generally, there's nothing that says you must constrain all your code to the confines of MVC logic. Gems are packages of Ruby code. If you find that you need a set of objects to handle complex business requirements, you could always package up that code as a Gem for use and distribution within your company. Gems are very easy to build and include in your projects, so the barrier of entry is very low.
I've been working with Rails since version 0.7. I've seen a lot of developers convince themselves that their problem doesn't fit in to the Rails box. While I always advocate using the right tool for the job, most people are wrong when they reach the belief that what they're doing isn't a good fit for Rails. It's usually an inability to understand where their ideas fit in to the broad scope of the MVC design pattern.
Having said that, there are plenty of ideas that don't fit Ruby on Rails, or MVC for that matter. Twitter was a great example. You don't implement a ultra-high-volume, low-package-size messaging system using an off-the-shelf MVC framework.
Very few problems are Twitter, however. Most business problems are 90% CRUD with a sprinkles of interesting work in between. Rails is a great fit for these types of projects because the "magic" peels away gracefully when you need it to.
There is another aspect of Rails that doesn't get much lip service. Rails is great because it makes a lot of decisions for you. The developers call this being "opinionated software". Maybe I'm just a little too humble, but a lot of the guys that work on Rails are bigger thinkers than I am when it comes to software frameworks. A lot of people won't admit it, but there is plenty of software that tries to copy the Rails way of doing things. That's at least some level of acknowledgement that they're things right. What I'm getting at is that there is a lot to learn from digging in to Rails on a deeper level.
The "Rails way" also extends beyond actual development and in to deployment strategies. Before we moved to Rails, we didn't have a sound deployment strategy. Rails developers just assume that you're going to use some type of SCM and deployment strategy. The most common being Rails/Git/Capistrano. If you say on the beaten path, there is a tremendous wealth of information available for all manner of deployment strategies. Again, these solutions fork for 99% of use cases. If you're a one-percenter, you're not going to benefit much from Rails, but make sure you've qualified your case and not just suffering from a case of "I'm smarter than the world."
That wraps up my thoughts on the matter. I'm sure there are other perspectives out there.
Why ROR? Speedy development, fun programming language, easy to build, test, iterate, repeat etc. Easy to migrate DB's, lots of scaffolding to reduce amount of code you need to write. Tons of great plugins written for the stack, very active community, some incredibly talented developers work on the stack.
ROR is perfectly suited for building out a great web app, quickly. And yes, it does scale ;)
The intention behind the design of ActiveRecord is that the db access logic is done automagically and you can make AR models your domain model.
The recent trend of adding various finders, etc. to AR models blurs the conceptual clarity a bit. Some plugins also blur this clarity.
Generally you should be able to use AR models as business objects, just be aware that there might be a few other behaviors added on that aren't strictly part of the business rules. This might irritate some people.
You are certainly free to write your business logic as separate classes as you see fit. Rails doesn't tell you not to. It just offers a convention that is often very reasonable for most peoples' needs.
cookies[:remember_token] = { :value => user.id, :expires => 20.years.from_now.utc }
Do exactly what you think they will, without having to define everything. That's the "magic" behind rails. It let's you focus on the application rather than the framework.
Where they differ, and this is from my experience (feel free to correct me) is that in MVC, the Views have an attachment to the Controllers. Basically a view can only have one controller. Views can have multiple partial views, but ultimately belong to one controller. In the n-tier system, the view can be abstracted to the point where it does not care what is going to be shown.
Once you wrap your head around MVC, it is hard to go back, especially for Web products.
ROR also takes care of a lot of your db schema and logic, so you can focus on programming, not writing CRUD statements.
I think the problem is that you're looking at "code samples", which tend to be the simplest possible examples. One of the nice things about RoR is that a model class can be as short as two lines:
and everything else happens by "magic". Our system has a few classes like that. OTOH, we also have model classes with thousands of lines of complicated business logic. But, showing those to someone trying to learn RoR would just overwhelm them.By the way: the Rails hype was 3 years ago. Now, its just one implementation of the same concept like all others.
However, this "magic" comes at a price, and at some point you will hit a wall, and if you're a very attentive programmer, you'll have been paying attention to what the magic has been accomplishing without you behind the scenes, and you'll be able to figure out a way around it without scrapping your code base. If you haven't done that, then you're going to pull your hair out in frustration.
To be honest, I'm not a huge fan of rails or other magic frameworks for anything other than prototyping, but ymmv, I definitely understand why people like it.
That isn't to say that Rails is a panacea, or even appropriate for projects of every shape and size. But if you're building an MVC web app, then it's really well thought out, allows you to be very productive, and in my dealings with it, those benefits don't go away, even as your application grows significantly in scope and complexity. YMMV etc etc...
Only that wall is at the end of a 50 mile runway. Don't let these sorts of pronouncements scare you ... by the time you get to this bogey man 'wall' you're going to be doing a million page views a day, dealing with replicated databases or just generally dealing with really big big problems. And even then you'll have a very supportive community of people who have had the same exact problem ... (example ... github coming up with Unicorn to help them server pages in a better way for them).
Long story short, for starting out and even a good ways into any application lifecycle. Rails is going to do you just fine.
Now, that can be overridden. You can call set_table_name to explicitly tell it to use a table, you can add additional methods, you can override the default setters and getters, etc. That's the idea behind Rails. You need a programming language to describe the decisions you're making that affect how your application works, but for a lot of things you're doing the same thing over and over and there's no reason to keep doing the same thing over and over.
Let's say that you wanted to (by default) order the articles by their creation date:
You've now defined some business logic so that when you call Article.all, you get them in that order. On the models, you can define any amount of logic you want by creating instance or class methods. In fact, all of those fancy keywords are just mix-ins - method calls that add methods to your class.This is why it looks like Rails is lacking a bit of business. When you call belongs_to :apple, the belongs_to method gets executed as the class is interpreted and that belongs_to method is something like:
Now, it's more complicated than that (since belongs_to adds some additional features), but that's the basic premise. You have this foreign key association and, most often, it's a simple association where you want to define a setter and a getter for that association. So, the belongs_to method takes the name and pops out two methods based on that name. The first is the getter and it makes the name you input into a camel-case and gets the constant with that name. It calls the find method on that constant and inputs the foreign key value to the find method. Likewise, the setter (setters in Ruby are methods that end with the equals sign) and sets the "name underscore id" attribute on the model to the id of the associated object.But you could just as easily define the methods yourself in the Article class rather than using the helper mix-in. Likewise, if you have business logic that doesn't fit in with what's already made, you can define any methods you want on your model classes.
The same goes for controllers. Controllers can be pretty small. If you're just trying to get the latest 10 articles, there isn't a lot you have to do - you need to fetch them from the database and store them in a variable. This is another one of those areas where Rails doesn't ask you for things it can assume. Instance variables (which start with the "@" symbol) get passed to the template, local variables (which have no prefix) don't go to the template. The template that gets rendered is the one named "controller/action". Of course, you can override that and call render :template => "xyz/abc" if you like.
I'm sorry if this seems a little all over the place, but I'm not sure what kind of business logic you're looking to implement. If you give me an example, I can provide some insight. However, from what you've said, it looks like you've just seen the basics of what models can do - and that's because a lot of the intro code is "let's build a blog" where there isn't a ton of business logic. But you can make any methods (class or instance) you like to do things to your data. Rails creates the basic cases for you - because the ...
There are a number of things I don't like about rails, and it's more important that in most languages to do lots of unit testing because of the extremely dynamic capabilities of ruby. You'll be happy when you do gem updates
[1] http://github.com/edavis10/redmine/tree/master/app/models/