20

For my next web application, I'm debating whether to use Rails 3.x or Sinatra.

I would like to use the server to provide user authentication, application-triggered emails, a fairly complex data model (behind ActiveRecord), and a JSON data interface with the web client. The client side will use static HTML, static CSS, Javascript/jQuery to render the JSON data into the views. The "policy" for rendering views will be driven by the Javascript code and some of the JSON data. I do not plan to use any dynamic view technologies such as ERB, HAML, or RJS.

Would I be better off with Sinatra or Rails 3.x?

Are there any other questions I should ask before making that decision?

Jay Godse
  • 15,163
  • 16
  • 84
  • 131

5 Answers5

26

Your data model is fairly complex, so I imagine your application will have to handle a significant number of business rules and interaction possibilities.

Sinatra is meant for handling lightweight software architectures. If you choose Sinatra you'll probably encounter design and organization issues you'll have to handle by yourself. Rails implements the MVC pattern and can help you organize your code by offering a lot of useful mechanisms.

You still can build a "full-stack" web app with Sinatra but you'll have to do a lot by yourself especially if the amount of functionality you'll provide is high (or will grow). I think Rails fits naturally better in larger architectures.

PS : ActiveRecord is available both in Sinatra and Rails.

John Topley
  • 113,588
  • 46
  • 195
  • 237
Antoine
  • 556
  • 3
  • 7
  • Given that I would use ActiveRecord in either case, why does it make a difference? After all doesn't ActiveRecord let you put business rules in the model layer? What would Rails' full MVC stack give you for business rules that Sinatra does not have? – Jay Godse Aug 30 '10 at 10:38
  • 2
    For example, you usually need validation rules in your model layer (not provided by AR). But the point is more general : in Rails you put yourself in a existing structure. You follow conventions and patterns which ensure your app will be easily maintainable and will scale well. Sinatra only helps you in routing and dispatching. In an large-size app you'll need more than this : you'll need separation of concerns and structuring patterns. Of course you can write a framework over Sinatra to ensure this but it takes time and will have bugs (like in every program). (You can also use Padrino) – Antoine Aug 30 '10 at 12:36
6

Sinatra would be a very good choice and I feel it would be better than rails mainly for one reason. This is linked with what another user wrote "You follow conventions and patterns which ensure your app will be easily maintainable and will scale well.

Some years ago we have developed a fairly large application in rails. All our developers had to do a complete rewrite because the rails core (read 37 signals) do not care to make their new version backward compatible. At every update the code would break.

So coding it in Rails will not ensure maintainability or scalability. Quite the opposite.

In fact scalability will be better on Sinatra as it is more lightweight and you would have anyway to use passenger for deployment. The Sinatra community is no-hype and extremely helpful. There are no prima donnas, big ego to the same degree of Rails. And this does matter because the Rails agenda might differ from yours and you might end up re-writing code very soon. There is a new book on Sinatra by O'Reilly that i think is worth a look as it is full of examples and ideas on what can be done and how.

devnull
  • 2,752
  • 1
  • 21
  • 38
1

Why would you ever use Rails for anything? Do you need template generators?

Writing an application with Sinatra or just pure Rack is just as easy as in Rails, and you get super speed for everything you do.

Sinatra apps can be organized in any way you want, and is much more flexible than Rails, making it the best choice no matter how large the application.

Making everything into micro-services and adding custom middleware feels much more natural with Sinatra and Rack.

People saying it's for smaller apps or architectures just haven't used it enough, I have no idea who started that myth.

Vidar
  • 1,008
  • 14
  • 16
1

I've got about the same task to do existing rails app that now needs a json interface for external devices (iPhone and Android apps). Highly suggest using sinatra-activerecord so you can just include all your models in your sinatra app. That way you don't have to rewrite your business logic.

Load your existing models in your sinatra app as follows: set :database, URI.encode( "#{db_settings['adapter']}://#{db_settings['username']}@#{db_settings['host']}/#{db_settings['database']}" ) require './data/models/user.rb'

And db_settings is loading it's settings from database.yml (same format as rails) use the yaml_db gem for that ;). This way your sinatra app can re-use all models and even the config files from the rails app.

Walter
  • 11
  • 1
  • If you're going to load your existing Rails models in Sinatra, why bother? Why not use Rails directly? What do you gain by going with Sinatra? – kakubei Oct 02 '12 at 08:46
0

You might want to take a look through the other lightweight Ruby frameworks before you decide - the post is a little old, but probably worth a scan.

Community
  • 1
  • 1
Dave Everitt
  • 17,193
  • 6
  • 67
  • 97