How Does Ruby on Rails Work

How does ruby on rails work?

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.

How do sessions work in Ruby on Rails?

When you request a webpage, the server can set a cookie when it responds back. You can read this article, it will clear you concept about sessions. http://www.justinweiss.com/blog/2015/03/17/how-rails-sessions-work/

How does Ruby on Rails work with an inclusion validation?

In your dev environment you will probably see that query running on any request which validates user, as code is reloaded on each request. Try it in production mode once, it should not happen in that case, as code is loaded once only and you haven't put the query in a lambda. There should be no lazy loading here as you already called map on the resultset.

How does the `extending` method work in ActiveRecord?

As extending saying -

Used to extend a scope with additional methods, either through a module or through a block provided. The object returned is a relation, which can be further extended.

Here is an little example to illustrate this functionality:

Spree::Order.class_eval do
def self.scope_cart
self.where(currency: "INR").extending do
def orders_in_cart
where.not(state: 'cart')
end
end
end
end

Spree::Order.scope_cart.count # => 367
# SQl code
# SELECT COUNT(*) FROM "spree_orders" WHERE "spree_orders"."currency" = 'INR'
Spree::Order.count # => 367

Till now, you can see that the scope_cart is giving me the data count which is the result from self.where(currency: "INR"). Well, let's see now what the new method orders_in_cart gives us.

Spree::Order.scope_cart.orders_in_cart.count # => 342
# SQL code
# SELECT COUNT(*) FROM "spree_orders" WHERE "spree_orders"."currency" = 'INR' AND ("spree_orders"."state" != 'cart')

Note : Please note the SQL code, for 2 different cases. There is the answer of your question.

Now coming to your example again. When you will call ModelName.page(1), you will get the result of this limit(default).offset(page * default). Now if you want the additional filtering, you will call per method as ModelName.page(1).per(5), then you will get the result from limit(num).offset(page * num), which is applied on the result of limit(default).offset(page * default).

Extending it further more :

When you do ModelName.page(1), limit is taking the default per value, with offset as 1 * default. Equivalent SQL is -

SELECT  "model_names".* FROM "model_names"  LIMIT 20 OFFSET 20

When you do ModelName.page(1).per(5), limit is taking the 5 as per value, with offset as 1 * 5. Equivalent SQL is -

SELECT  "model_names".* FROM "model_names"  LIMIT 5 OFFSET 5

In Rails, I saw in the documentation if you write like User.limit(10).limit(20), it generates SQL has LIMIT 20, means it take the last limit clause in the chain. Same is true for the offset.

Look the console generated SQL code :

Spree::Order.limit(20).offset(2).limit(5).offset(5)
# Spree::Order Load (0.5ms) SELECT "spree_orders".* FROM "spree_orders" LIMIT 5 OFFSET 5

what is ruby on rails?

The Language

Ruby is a recent programming language that shares most of its heritage with Perl and Smalltalk. You can see what Ruby looks like (and try it yourself) at tryruby.org.

Just like you can use PHP to write web pages, you can do the same thing with Ruby.

The Framework

Ruby on Rails is a set of software devices that help you to more easily write a website in Ruby. The primary things they try to facilitate ("make easy") are:

  1. Storing related data (e.g. blog posts and the comments on them) to a database.
  2. Accepting web requests and respond to them programmatically (e.g. check a user's password)
  3. Composing HTML using your data, with layouts and templates to make it easier.

"Rails," as it's called for short, is built with extreme prejudice towards certain application models, particularly MVC. What this means is that unlike PHP, where any .php source file is fair game for any bit of code, most code in a Rails application is written in a particular, conventional place. When people compare Rails to PHP, they often point this out.

There's a lot I didn't cover, but these are Rails' most basic features. To see what it looks like, I'd suggest watching the infamous, "Creating a Weblog..." screencast.

Setting up Ruby on Rails for web development

Any OS where Ruby on Rails is optimal or any general advice I should know

Linux and Mac OS are by far the most popular dev OS's for Rails - you can install dependencies much easier.

Windows can be a real pain with Rails at first. Not rails, but its dependencies. These are called Gems and work like plugins.

Certain gems require external binaries / C headers for them to run. Two of the most notorious are the MYSQL2 and RMagick (ImageMagick) gems.

--

I don't know the core problems (it's something to do with MINGW32 / Win32 environment), but Windows often has a difficult time with certain gems.

Not that it's stopped us - we have a series of Windows machines devs use to create truly incredible RoR apps (very difficult to get them all running smoothly).


DB

You don't get a DB with your Rails install - you need to have that running separately.

If you don't want to install a local MYSQL server (which can be a pain), I'd recommend getting some cheap shared hosting (with unlimited MYSQL db's). This will allow you to hook up your app to a third party DB host, negating any data integrity issues if your dev machine has problems.

Make sure you have PHPMYAdmin too - it's a life saver :)

Although Rails with many different SQL variants, MYSQL is the most popular. Heroku has made PGSQl quite popular within the Rails community, although I'm not up to speed on the major differences.


how does Ruby on Rails work with a local server

Rails comes with its own web server for development, called WEBrick.

Sample Image

Honestly, WEBrick is a pile of shit but it works.

You just need to load up the cmd, type rails s and it will fire it up. If you get some more experience, you can use the likes of puma or thin as dev servers (they are much faster and better mimic the production environment)

In production, you'll be best using one of the commercial-grade servers like Apache or NGinX, good tutorial here.


Getting Started

There are a ton of tutorials to get started with Rails.

Some of the best resources are:

  • RailsCasts (stopped mid 2013)
  • GoRails (still active)
  • Michael Hartl Rails Tutorial

Finally, enjoy yourself. I remember the Tumblr guy saying that he got such initial traction for Tumblr through the Rails community (he said they're the most committed bunch ever). He was right, the Rails community is a big family who all love code.

Sample Image



Related Topics



Leave a reply



Submit