RubyFlow : The Ruby Community Blog

Home   Submit   Sign Up   Log In   leaders   Twitter   RSS Feed  
 

wjgilmore — 12 posts

http://easyactiverecord.com

The fantastic Devise authentication gem asks registering users for just two items: an e-mail address and password. Chances are you'll want to subsequently gather additional details, such as the user's name or location, typically done through an account profile management form. In this post I show you how this is accomplished.
A fundamental Active Record feature known as eager loading regularly trips up new Rails developers to the detriment of their application's performance. In this post I offer a concrete example illustrating the difference between the includes and joins approaches, complete with a log of the queries executed when each approach is implemented.
Rails bundles default 404 (file not found), 422 (unprocessable entity), and 500 (internal server error) pages into every newly generated application. While they get the job done, these pages are pretty bland, so in this post I’ll show you how to update them to suit the logic and design requirements of your application.
I use the Rails console repeatedly throughout the day, but peering into object data can be a bit difficult due to poor formatting. Fortunately, a great gem called Awesome Print can bring an end to the formatting blues. In this blog post I introduce Awesome Print, and show you how to integrate it into your Rails console.
I'm very pleased to announce the release of a book I've been working on for some time now, "Easy Active Record for Rails Developers". The book covers everything under the sun pertaining to Active Record, including model and schema management, validations, queries, data management, extensive coverage of associations, nested forms, model testing using RSpec and FactoryGirl, and much, much more. Readers learn through walkthroughs of many examples based around a real-world project called ArcadeNomad that tracks the location of retro 1980's arcade games.
It is often useful to know how many records are associated with a model identified as being the parent in a belongs_to relationship. Active Record's default behavior is to determine this number using a COUNT(*) query. Fortunately, you can avoid this additional costly query by caching the associated record account with a few simple modifications to your Rails application. Read the tutorial.
Changing the Rails Console Prompt  easyactiverecord.com
The Rails Console is an indispensable tool, providing developers with an easy way to experiment with models, Ruby syntax, and pretty much any other code associated with a Rails project. But the default prompt is a bit overbearing, including by default the Ruby version and number of commands executed in the current session. You can easily simplify the default prompt by following the instructions found in this easy tip.
When working with Ruby and Rails, a common source of confusion involves mixing up nil?, blank?, and empty?. All three methods clearly deal with determining whether a data structure is assigned a value, but there are different degrees of nothingness, and in the world of programming those degrees are important. This blog post helps to dispel the confusion by working through a number of different examples.
Paginating database results is a standard feature of most web applications, yet isn’t something you’d want to necessarily implement on your own. Fortunately, Rails developers have a whole host of options at their disposal, including the will_paginate gem. This post shows you how to easily integrate will_paginate into your Rails application. Read the post.
The attr_accessible macro has long been a Rails mainstay, playing a major role in preventing malicious access of sensitive model attributes. However it (and its sibling attr_protected) was deprecated in Rails 4 in preference of a new approach known as "strong parameters". In the latest Easy Active Record blog entry I introduce strong parameters, explaining why attr_accessible was inconvenient and demonstrating the advantages gained by the new strong parameters approach.
Many applications require the retrieval of random database records (for instance to highlight a random product in an online store). There are several ways to accomplish this task, but efficiency varies widely and becomes more pronounced as your database grows in size. This blog post demonstrates two common approaches when using Rails Active Record and MySQL, and benchmarks both (using the Ruby Benchmark module) to demonstrate their efficiency.
In the runup to the publication of my new book, "Easy Active Record for Rails Developers", I'm publishing a series of posts covering commonly confusing or little-used Active Record features. The latest discusses two approaches to creating default Active Record model attribute values.