Get ready for your next interview with our comprehensive question library
Ruby on Rails is a server-side web application framework written in Ruby that follows the Model-View-Controller (MVC) architectural pattern. MVC separates the application into three interconnected components:
Rails emphasizes convention over configuration and DRY (Don't Repeat Yourself) principles, making development faster and more maintainable.
The main Rails directories include:
app/
- Core application code (models, views, controllers, helpers, assets)config/
- Application configuration, routes, database settingsdb/
- Database schema, migrations, seedslib/
- Extended modules and custom librariespublic/
- Static files directly served by web servertest/
or spec/
- Test filesvendor/
- Third-party codeGemfile
- Gem dependencies specificationSymbols are immutable and stored in memory only once, while strings create new objects each time. Rails prefers symbols because:
# Symbol - same object_id
:name.object_id == :name.object_id # true
# String - different objects
"name".object_id == "name".object_id # false
Convention over Configuration means Rails makes assumptions about what you want to do and how you'll do it, rather than requiring you to specify every detail. Examples include:
User
maps to database table users
UsersController
handles /users
routesid
table_id
This reduces the amount of code developers need to write and maintains consistency across projects.
Migrations are Ruby classes that allow you to evolve your database schema over time in a consistent way. They provide:
class CreateUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
t.string :email
t.timestamps
end
end
end
User.find(1) # Raises error if not found
User.find_by(email: "a@b.com") # Returns nil if not found
User.where(active: true) # Returns Relation (chainable)
def create
@user = User.new(user_params)
if @user.save
redirect_to @user # New request to show action
else
render :new # Renders new template with @user errors
end
end
Rails console options:
rails console
or rails c
: Standard consolerails console --sandbox
: Rolls back changes on exitrails console production
: Production environment consolerails dbconsole
or rails db
: Direct database console# Sandbox mode - all changes rolled back
rails console --sandbox
# Production console
RAILS_ENV=production rails console
Rails provides three default environments:
# Check current environment
Rails.env.development? # true/false
Rails.env # "development"
# Environment-specific code
if Rails.env.production?
# Production-only code
end
# Custom environments
RAILS_ENV=staging rails server
Each environment has its own configuration file in config/environments/
and database configuration.
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime