# Best Practices for Project Organization in Rails
This tutorial provides an in-depth look at the best practices for organizing your Rails project. By maintaining a clean and manageable codebase, you can greatly improve the overall quality of your project, making it easier to build on in the future.
Through this tutorial, you will learn how to structure your Rails project effectively, how to organize your files and directories, and how to manage your codebase efficiently.
Prerequisites: Basic understanding of Ruby on Rails and the MVC architecture is required.
A well-structured project is easy to understand and maintain. In Rails, there are some conventions to be followed:
app/
- Contains the core application (models, views, controllers, helpers).config/
- Contains configuration for the Rails environment, database, and more.db/
- Contains everything related to the database (migrations, schema).test/
- Contains all test files.Keep your controllers 'skinny'. Controllers should only be responsible for handling requests and responses. Business logic should be kept in models.
# bad practice
def create
@user = User.new(params[:user])
@user.save
redirect_to @user
end
# good practice
def create
@user = User.create(params[:user])
redirect_to @user
end
In Rails, organization of files and folders is crucial. Here's an example of how to organize your models:
# app/models/user.rb
class User < ApplicationRecord
# User model logic
end
# app/models/concerns/nameable.rb
module Nameable
extend ActiveSupport::Concern
included do
# shared logic for nameable models
end
end
Keep code DRY (Don't Repeat Yourself). Use modules and concerns to share code among models.
# bad practice
class User < ApplicationRecord
def full_name
"#{first_name} #{last_name}"
end
end
class Author < ApplicationRecord
def full_name
"#{first_name} #{last_name}"
end
end
# good practice
# app/models/concerns/nameable.rb
module Nameable
extend ActiveSupport::Concern
def full_name
"#{first_name} #{last_name}"
end
end
# app/models/user.rb
class User < ApplicationRecord
include Nameable
end
# app/models/author.rb
class Author < ApplicationRecord
include Nameable
end
In this tutorial, we learned how to effectively structure and organize a Rails project. We also discussed the importance of keeping your controllers thin and your models fat, and how to use modules and concerns to keep code DRY.
To learn more about Rails, check out the official Rails guides.
Solutions:
# 1
# Navigate to your working directory and create a new Rails project
$ rails new blog
# 2
# app/models/concerns/printable.rb
module Printable
extend ActiveSupport::Concern
def print_details
attributes.each do |attr, value|
puts "#{attr}: #{value}"
end
end
end
# app/models/post.rb
class Post < ApplicationRecord
include Printable
# Post model logic
end
# app/models/comment.rb
class Comment < ApplicationRecord
include Printable
# Comment model logic
end
Test your models in Rails console:
# Create a new post
post = Post.new(title: "Post title", content: "Post content")
post.print_details
# Create a new comment
comment = Comment.new(content: "Comment content")
comment.print_details
Keep practicing by creating more models and modules. Happy coding!