Easy Active Record for Rails Developers (The Book + ArcadeNomad Project Source Code)
Easy Active Record for Rails Developers
Master Rails Active Record and Have a Blast Doing It!
About the Book
Easy Active Record for Rails Developers is the ultimate guide to mastering building database-driven Rails applications using the powerful Active Record ORM Framework. By loosely following along with the development of a fun, web application for retro arcade aficionados, you’ll learn how to:
- Understand the importance differences between and implement the belongs_to, has_one, has_many, has_and_belongs_to_many and has_many :through model associations
- Properly validate, filter and manipulate your application’s model data
- Manage model schemas using Active Record’s powerful migrations tool
- Create and integrate complex web forms, including those involving nested models
- Deftly traverse associations using includes and joins
- Monitor, debug and optimize your queries
- Integrate user registration and authentication features using the Devise gem
So far, I’m liking the no fluff, straight to the point approach! - Don Pawlowski, Developer, Designing Interactive
Table of Contents
Easy Active Record for Rails Developers is divided into six chapters covering the fundamental topics most critical to today's Rails developer.Chapter 1. Introducing Models, Schemas and Migrations
In this opening chapter we'll get acquainted with many of the fundamental Active Record features you'll use throughout your project's lifetime. You'll learn how to create the models used to manage the application's business logic and provide convenient interfaces to the underlying database. You'll also learn how to use a great feature called *migrations* to create and evolve your database schema. The chapter concludes with a section explaining how to begin testing your models with RSpec and factory_girlChapter 2. Loading, Validating and Manipulating Data
In this chapter you'll learn how to easily load, or *seed*, your application with an initial data set, as well as create and manipulate data. Of course, because we'll want to eliminate all possibilities of incomplete or invalid data from being persisted to the database, you'll also learn how to enhance your application models using a variety of *validators*. Additionally, we'll build on the introductory model testing material presented in the previous chapter. Notably, you'll learn how to create tests for ensuring you've properly fortified your models with enough validators to ensure unwanted data can't slip through the cracks.Chapter 3. Querying the Database
In Chapter 2 you learned how to load seed data and create records, now it's time to begin querying that data! In this chapter I'll introduce you to Active Record's array of extraordinarily powerful query features. By the conclusion of this chapter you'll know how to efficiently query for all records, retrieve a specific record according to primary key, select only desired columns, order, group and limit results, retrieve random records, and paginate results. You'll also learn how to integrate these queries with the application controller and view to create listing and detail pages. Always striving towards writing readable code, we'll also enhance the models using a fantastic feature known as a *scope*.Chapter 4. Introducing Associations
Building and navigating table relations is an standard part of the development process even when working on the most unambitious of projects, yet this task is often painful when working with many web frameworks. Not so with Rails. Thanks to a fantastic feature known as *Active Record Associations*, defining and traversing these associations is a fairly trivial matter. In this chapter I'll show you how to define, manage, and interact with the `belongs_to`, `has_one`, `has_many`, `has_many :through`, and the `has_and_belongs_to_many` associations.Chapter 5. Mastering Web Forms
Your application's web forms will preferably interact with the models, meaning you'll require a solid grasp on Rails' form generation and processing capabilities in order to integrate with your models in the most efficient way possible. While creating simple forms is fairly straightforward, things can complicated fast when implementing more ambitious solutions such as forms involving multiple models. In this chapter I'll go into extensive detail regarding how you can integrate forms into your Rails applications, covering both Rails' native form building solutions as well as several approaches offered by popular gems.Chapter 6. Debugging and Optimizing Your Application
While it's fun to brag about all of your project's gloriously cool features, they came at great expense of your time and brainpower. Much of the effort was likely grunt work, spent figuring out problems such as why a query was running particularly slow or the reason a complex association wasn't working as expected. This important but decidedly unglamourous part of programming is something I try to minimize on every occasion by relying on a number of tools and techniques that can automate much of the analysis and debugging process. In this chapter I'll introduce you to several of my favorite solutions for debugging and optimizing Rails applications.Chapter 7. Integrating User Accounts with Devise
Offering users the opportunity to create an account opens up a whole new world of possibilities in terms of enhanced interactivity and the opportunity to create and view custom content. Yet there are quite a few moving parts associated with integrating even basic account features, including account registration, secure password storage, sign in, sign out and password recovery interfaces, and access restriction. Fortunately the fantastic Devise gem greatly reduces the amount of work otherwise required to implement account creation, authorization and management features, and in this chapter I’ll introduce you to many of the wonderful features this gem has to offer.Chapter 8. Powerful Active Record Plugins
Ruby on Rails is such a powerful solution thanks in large part to its enormous ecosystem. You're probably familiar with prominent gems such as Devise, RSpec, and FriendlyId, but there are also much more focused gems capable of extending specific parts of Rails, including templating (such as Redcarpet) and e-mail (see the popular Mail gem). So too can you extend Active Record in a variety of interesting and useful ways, and in this chapter I'll introduce you to a few of my favorite gems, including Acts As Taggable On, Decent Exposure, and Geocoder.
The Book + ArcadeNomad Project Source Code
This package includes the book in addition to *all* of the source code and sample data used to power the ArcadeNomad demo project.
This package includes the book, and of course all updates are free to existing readers forever!
- Introducing Active Record
- About this Book
- Introducing the ArcadeNomad Theme Project
- About the Author
- Errata and Suggestions
Chapter 1. Introducing Models, Schemas and Migrations
- Creating the ArcadeNomad Project
- Installing the RSpec, FactoryGirl and Database Cleaner Gems
- Introducing Rails Models
- Rails Models are Plain Old Ruby Objects
- Introducing Migrations
- Testing Your Models
Chapter 2. Loading, Validating and Manipulating Data
- Seeding and Updating Your Database
- Introducing Rails Validators
- Creating, Updating, and Deleting Records
- Deleting and Destroying Records
Chapter 3. Querying the Database
- Finding Data
- Paginating Results
- Creating a Prettier URL with FriendlyId
- Introducing Scopes
Chapter 4. Introducing Associations
- Normalizing Your Database
- Introducing Associations
- Eager Loading of Associations
- Polymorphic Models
Chapter 5. Mastering Web Forms
- Web Form Fundamentals
- Rails Routing and Forms
- Creating a Simple Contact Form
- Managing Your Application Data
- Useful Forms-Related Gems
Chapter 6. Debugging and Optimizing Your Application
- Debugging Your Application
- Optimizing Your Application
Chapter 7. Integrating User Accounts with Devise
- Installing Devise
- Creating the User Account Model
- Adding User Registration Capabilities
- Adding User Sign In and Sign Out Capabilities
- Adding User Account Management Capabilities
- Adding Password Recovery Capabilities
- Restricting Access to Authenticated Users
- Commonplace Devise Customizations
Chapter 8. Powerful Active Record Plugins
- Tagging Your Content Using the Acts As Taggable On Gem
- Cleaning Up Your Controllers with the Decent Exposure Gem
- Geocoding Your Models
The Leanpub 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
80% Royalties. Earn $16 on a $20 book.
We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $12 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.