For Heaven’s Sake, At Least Test the CRUD

The very first integrated (feature) specs I setup it is to test the CRUD. If you don’t do anything else with feature specs, please, at least do these tests. You will be amazed at how much of your application is actually exercised and covered just with these simple tests.

Yes, you can and should test much more. And they don’t replace good unit and integrated tests.  But you will be surprised how many bugs these basic tests will catch. For example, my nemesis, the dreaded undefined method `blahblahblah' for nil:NilClass  in views.

These test will ensure the Adult model will:

  • Display all views (this alone is huge)
  • Data can be entered, displayed, updated, and destroyed (CRUD)
  • Model validations are respected
  • Potentially test model relations

I am going to show you how to test #index, #show, #edit, #update, #new, #create, and #destroy of a resource, an Adult model. I’ll break it down because the full spec file is long.

Here is the entire spec in it’s original project: adults_spec.rb


A little pre-knowledge would help; we are using Devise (for authentication), Rspec (for testing framework), Capybara (for integration tests). Note, we are not testing JavaScript in this test, and therefore invoke the RackTest as the Capybara driver for maximum speed.

Warden.test_mode!  gives us some nice Warden helper methods to use during testing. Notably the login_as(user, scope)  method. Warden is used under-the-hood by Devise.

seed_all_records  is just a method that will create a complete structure of records so that we have something to find and display. For example, it could create a user, company, orders, invoices, inventory, etc.

IMPORTANT: I am loading these records in a before(:all) do  block so that they are not recreated for every test. If  seed_all_records  creates a long and complex amount of records, as this real-life version does, it can dramatically speed up the test.

CAVEAT: If we need to create other new records, we need to manually keep track of them to delete them after use. See the #edit example.

For every test, the first thing we do is view the index,  visit unit_adults_path(@unit) . I like to start here and “click” the appropriate links to edit/show/delete tests.


So we take a look at the index page, a table with a list of names (adults) and we check that we see some basic data (links and text).


We view the show page by clicking a link from the index page (Adult name). First, we make sure we are looking at the right show page. Then we click the “Return To List” link and make sure we go back to the index page. Finally, we try the “Edit” link of the Adult name and make sure were are taken to the edit form.

We look at the page.current_path  and test it to make sure we are on the right page. This may not work in your application as some paths may contain query values or other errata. But for simple specs with standard Rails routes, it works.

 #edit, #update

Like I noted above, we will need to create an Adult because we are going to change their data and we don’t want to affect the existing records. In an after do  block, we will delete this Adult.

We create this new Adult, then we make some changes in the edit form. But don’t submit yet! We’ll do that later.

In the first test, we submit the change, and make sure that any form changes we make are reflected on the Adult’s show page. Success! We can edit and submit Adult records. This tests a significant amount of the Adult model, validations, and potentially more (relations, counters, etc).

Lastly, we test that if the user cancels the edit, they are returned to the Adult show page and none of the entered changes are saved.


“I brought you into this world, I can take you out.”

Create a new Adult, then revisit the index page and click on the new Adult’s destroy link, and make sure they are gone and return to the index page. We also check that the flash notification is displayed.

Note: if you use a confirm: 'Are you sure?'  like Rails suggests as a default for destroy links, because we are using RackTest, it will not execute that JavaScript. Thus there is not confirmation dialog to click.

 #new, #update

Saved the longest for last. Also, the most important.

We are going to test two contexts: entering valid data, and entering invalid data.

Enter Valid Data

Simple, we click on the New Adult link, enter a bunch of data into the form, and save it. Then we verify that we are taken to the show page and can see all the data we entered.

This particular form has several select elements that allow the user to select from a list of options, as well as establish relations to other models. This is a good place to test this, but it could also be tested in #edit, #update. We verify that this Adult can have related Scouts, and no related Scouts. This also tests your models, validations, and relations. Good tests to have.

Enter Invalid Data

We want to see that when we enter invalid data, that model validations are respected, and that the record is not saved. When trying to submit invalid data, we return to the new form and check that the proper flash message is displayed.

In this example, I only test one field’s validation, that the Adult’s first_name can’t be blank. This is an excellent place to add more test for more validations.


Put it all together, and you are testing a lot. I know they seem like simple tests, maybe too simple for some. But I think they are boilerplate for much more.

I said it before, but it bears repeating: setting up these basic CRUD integrated tests will catch a surprising amount of bugs.

Posted in Rails, Rspec. Tagged with , .

Creating Cache Keys for Rails Relations

For years I have been using the cache_key_for_*  method, as seen in Rails Guides and many other places. Here is an example.

It works. But even this venerable snippet couldn’t escape the refactoring razor.

.size vs .count

First change is using products.size  instead of products.count . A minor change, but if ActiveRecord has already executed the sql query, using the .size  method will prevent it from executing another query, as .count  may cause.

Sean Griffin has a quick say in this battle. TL;DR Rails users should always use .size .

DateTime Number

Second, the original used a long chain of trys and then converting the DateTime of updated_at to number format. You might be breaking the law… of Demeter that is.  That ‘number’ format looks like this: 20150410030422. It’s just the date and time.

Well, you can accomplish the same thing without chaining so many methods: use .to_i. Read about it at the docs. This one method uses the UTC time and gives you the number of seconds since the Unix Epoch. It looks like this: 1428635331.

Both has the same 1 second resolution, but the refactored version is shorter, which may save memcached memory if you have a lot of keys. I just think it’s cleaner, and avoids all the method chains.


Finally, we just need to assemble it all in a string. By putting the size and time value into an array, we can use all those wonderful enumerable methods. You could just as easily use string interpolation and avoid the map and join.

Important Footnote

Rails has been using nanosecond resolution for their cache_key method for a while, assuming your backend supports factional second datetime values. My method only has 1 second resolution. If you require more granularity for your cache keys, by all means, use the original cache_keys_for_*  method and change the .to_s(:number) to .to_s(:nsec).

Posted in Rails. Tagged with .

Create an In-memory Temporary ActiveRecord Table for Testing

I was reading Greg Niewisiewicz’s blog post on 6 Gems to Make Rails Development Easier and saw the first recommendation was for a gem called temping. Nice. But here is my take on the same thing without adding a gem (sort of) and doing it with in-memory SQLite tables.

I’m using Rspec3, so these example will need modification if you are using MiniTest.


Ok, so you will need to add the sqlite3 gem to your Gemfile, if you don’t already have it in there. Because I am only using SQLite for testing, I put it in the :test group.


We need to create the table in SQLite via migration, then tell ActiveRecord to switch it’s database connection (temporarily) to use this in-memory db as the test environment database. We also add the ActiveRecord class so that we can, well, CRUD records.


We need to switch back to using your “test” database after all the tests are done in this spec. DON’T FORGET THIS STEP! Or all your other tests may fail, and you will be scratching your head why.

Good To Go

So now, you can operate on your temporary, in-memory, ActiveRecord table at lightning speed. And you don’t even need a gem. Just a few lines added to your test.


Well, there is one downside, by using SQLite for the spec, you may not be using the database you would be running in production. This is true, unless you are using SQLite in production.

But, to be honest, you should try to be as database agnostic as possible. What if you have to switch from MySQL to PostgreSQL in the future for your production backend. Any MySQL database specific language you are using may fail in PostgreSQL.

On the plus side, the in-memory SQLite tables are really, really fast.

Posted in Rails, Rspec.

Why Rails and Their Committers Are Awesome

I love keyword arguments when they were introduced in Ruby 2.0, and use them every chance I get. As I was hacking around with ActiveJob today, and thought it would be cool to pass keyword arguments into the ActiveJob#perform_later method.

I thought it should work. It didn’t.

You get an  ArgumentError: wrong number of arguments (1 for 0) error. The reason is the hash is converted to string keys upon serialization.

So I was thrilled when I did a bit of searching to see if anyone else had the same issue. Nope. Looked at the ActiveJob::Arguments module. Passing keyword arguments are not supported. I actually get to report a real feature request to Rails!

So here is my ActiveJob should support passing of keyword arguments to perform method.

This this is where the magic began… within a few minutes Sean Griffin (@sgrif) picked up on the issue report. I went to lunch, planning to fork ActiveJob and start hacking away when I got back. But Sean beat me to it. Sean added the ability to pass keyword arguments to ActiveJob jobs, and even backported it to Rails 4.2.1.

Within an hour, it was all done. Check out the full commit.

Want a reason to love Rails and their committers? This is one, of many.

Thank you Sean.

Posted in Rails, Ruby. Tagged with , .

Self-joined HABTM with Cache Updates

You have a self-joined HABTM table. It’s probably called User too. All is well, until you realize that when relationships change, your site does not display those changes. The reason is that the related objects (users) are not being updated when the relation is created/removed or one of the objects are updated/destroyed. And the cache keys that rails generate for the parent object are still referencing old data.

Here is how to fix it.

Self-Joined Users

Pretty simple really. There is a table in the database called  user_relationships with two fields:  adult_id ,  child_id . You can create it with this migration:

Now every User can have  .adults and  .children. And those adults and children can have adults and children. And so on. And so on.

User Views

You are using rails fragment caching aren’t you? If not, stop right now and read up on how to implement caching in your rails app because even if you don’t need it now, you most like will need it later. Your users will love if for it!

Watch more: Greg Pollack has an excellent video on Dalli & Cache Digests. He does a much better job explaining caching, cache keys, and fragment caching that I.

Rails uses the concept of a cache_key for each object. Cache keys are simply the model name, record id, and the datetime of the  updated_at attribute for the ActiveRecord object. Check it out:

Where cache keys come into play are in views. When a view generates it’s html, the cache  method will ask your caching system (most likely memcached) if a key exists. If it does, then it pulls that fragment from the cache without the expense of generating the html. If they key doesn’t exists, then it renders the html and then stores the result in the cache. Here is a very simple example.

When rendered, this will display the user’s name, and the names of all their children.

Changing Relationship Doesn’t Change the Rendered View

All is going well, until you start getting reports of problems. A user deletes one of her children, but when she views her show page (see above view), she still see the deleted child’s name. The reason is that when you make a change to a HABTM relationship, it only removes the relationship in the uses_relationships  table. It doesn’t update the  updated_at datetime for the parent record. Thus, the cache key for the parent record does not change, and the old (incorrect) information is read from cache.

How To Fix

I wan’t able to find an definitive example of how to fix this completely and correctly for all cases. So here is my implementation.

What you need to do is “touch” all related objects in a relationship anytime that relationship changes. And those changes could be:

  • Parent (adult) object is updated.
  • Any children objects are updated.
  • Relationships are created.
  • Relationships are deleted.

To fix the first two cases, we just need a couple of callbacks.

Astute coders will notice that we used update_all instead of  touch here. While we could have used touch, there is the possibility that you could create a circular update infinite loop. The touch  method will cause all touched objects to also execute any touches that are defined on their relationships.

In our particular case, we are not concerned about touching any additional relationships here because were are only updated the updated_at  attribute to trigger a cache invalidation. We aren’t actually changing the related objects data like a name or birthday.

Now, we need to fix the last two cases when relationships are created or deleted.

Note that we added  after_add: :touch_updated_at, after_remove: :touch_updated_at  to the HABTM definition. These are callbacks that are triggered upon relationship additions or deletions. They both call the touch_updated_at  method that like above, will update the updated_at  attribute. But in this case, the HABTM will pass a reference to the modified user object.

Here we use the update_column  method because we do not want to trigger any callbacks, validations, or cascading touches.


Now, anytime a user adds a child, deletes a child, or any data on all related children is changed, the updated_at  attribute is set to the current datetime. This will change the calculated cache key for the user objects, and voilà, the view will regenerate the html and users will see the modifications.

Posted in Uncategorized.

Rails HABTM with Unique Scope and Select Columns

This one left me scratching my head for a while. Not quite sure I would call it a bug, but this one definitely has a GOTCHA.

Let say you have a Users model and Units model. Each can have_and_belong_to_many  of each other. But, you want to make sure there are no multiple User->Unit relationships, or Unit->User relationships. So you use the association scope -> { uniq } .

It won’t work. Here is why.


So we have these models:

And the relationship table:

You can read more about ActiveRecord associations and queries at Rails Guides.


When you query a Unit for Users, you get only DISTINCT users, and vice-versa.

But All is Not Well

Let’s check out what happens if we use a .select(:first_name)  with the Unit.users query.

Whoa there… our count of users went from 130 (the correct number) to just 108. Why? Take a look at the SQL with the .select(:first_name)  query. Specifically the  SELECT DISTINCT COUNT(DISTINCT "users"."first_name") . The query that ActiveRecord is generating is doing a DISTINCT (alias for uniq) on the "users"."first_name" . This query will only find users with DISTINCT first names. What if you have 3 people with the first name of “John”. Well, it will only find and return 1 of those John’s.

This is not what I expected. POLA any one?


Rails Guides has a good section on Selecting Specific Fields that goes into detail about using .select()  with .unique . But it doesn’t mention that using the -> { uniq }  scope on an ActiveRecord association will have the same affect. Well, it does.

So, it got me to thinking… could I add -> { distinct(:id) }  to the HABTM declaration? Yes you can. But it doesn’t work with .select() . For some reason when you use .select()  ActiveRecord will override any column declarations you used with the distinct() .

Proof eh?

I find it interesting that the first query, without specifying a .select()  generates the correct DISTINCT query with the "users"."id"  column. But as you can see on the second query, it is overriding the specified scope with the .select()  columns.

This is my logic: if I use .select()  that doesn’t necessarily mean I want DISTINCT columns, regardless if weather I specify a .distinct()  or not. In the above case, .select()  always includes the :id column wether or not you are also using .distinct() . So why would the .select()  override my .distinct(:id)  declaration?


First solution is to not use .select()  with a query. Duh.

But I really like .select() . It’s a great way to reduce memory size when instantiating hundreds or thousands of User ActiveRecord objects. And in my real-world use case, the User table has a couple of text columns that contain large amounts of html. So I like to use .select()  where ever I can.

So I guess there really isn’t a solution that I know of, except not to use .select()  with ActiveRecord queries on HABTM tables that use the -> { uniq }  scope.


Best Solution!

You knew it was coming, didn’t cha. I wouldn’t leave you hanging.

The best solution is to not use the -> { uniq }  scope with HABTM relations, but enforce the uniqueness condition at the database level with unique compound indexes.

In your migration for the relationship table just add unique: true  to the index declaration.


Posted in Rails.

Model Doesn’t Save After Adding before_save Callback

File this one under the Duh folder… I added a new before_save callback yesterday, and suddenly nothing in the model would save. Here is the offending code:

Spot the problem? I didn’t. At least for a while.

In a callback, if you return  false the save is cancelled. I knew that. So, when setting the sms_number_verified  attribute to false , it also returns false  from the callback, and therefore the model does not save. I did not expect that. Seems evident now that you see it.

Here is the fix:

Just return true from the callback.

Would have been nice if rails notified me in the logs with something like “save cancelled, before_save callback returned false on line XXX”.

Posted in Rails.

SQLite3::ConstraintException: table.created_at may not be NULL

So, I’m working along in Rails today, added a new migration, when all of a sudden tests that were passing only a few minutes ago suddenly start failing. The error I in rspec is something along the lines of:

In test.log:

WTF? This was working only a few minutes ago.

If you google this, you will read endless others who experience the same error… when using HABTM relations. Well, EventSignup has no HABTM!

This has happened to me a couple of times, and each time it leaves me puzzled for far too many minutes, and there is no real solution.

Okay, well there is only one solution I have found to work: delete the SQLite database file and recreate. Yeah, that sux. Oddly enough, just a quick:

And all is good again.

If anyone has an idea why this happens, I would love to hear why.

Posted in Rails. Tagged with , .

Rails Nested Attributes with Scoped Uniqueness Validation of Association

This is going to be a long one, so grab your favorite beverage…


I have a fairly standard belongs_to  and has_many model association. And I want to save nested attributes along with the parent when creating a new parent record. But, I don’t want the user to be able to submit duplicate values for the child records.

Sounds easy. That’s what I thought when I started.

My Solution


We have an Organization and a SubUnit models, pretty standard stuff here.


The controller is simple (standard actions removed). Keep scrolling down.


Now it’s getting interesting.

Your form should look like this.


Polluting Links with Javascript

Like everyone else who has started down the road to nested attribute bliss, you have watched Ryan Bates’ Railscast on Complex Forms. It’s superlative.

But there’s one thing I didn’t like… the heavy handed approach of using link_to_function. Especially when including a fair amount of escaped html into an onclick script. I’m not saying it’s wrong, just that I believe html should stay where it belongs… in the DOM. Heck, they even created a gem nested_form.

I think we can make it simpler, painless, elementary.

That’s where the following little ditty comes in handy. We are creating the exact same field structure that is used for entering a SubUnit, but hiding the html so that we can use JavaScript to copy-and-append to the visible entry area.

Line #1: hide the div.

Line #2: use fields_for to generate the proper field id and name attributes so that it is submitted as a child object to our Organization. Notice the That prevents this block from iterating over all the @organization.sub_units that may be built, either as a new object (controller line #5), or when re-rendered due to a validation error with @organization.

Line #2: don’t forget the child_index: ‘new_sub_unit’ option! Without it, rails will attempt to index the fields id and name attributes with numbers, which in this case would be 0 (zero), and mess up the prior sub_unit fields. IOW, you will end up with two params[:organization][:sub_units_attributes][0] elements.

If you don’t understand all the details, take a few minutes and read the ActiveRecord::NestedAttributes::ClassMethods.

Now for some JS magic:

Pretty simple, bind to the click events of the add-sub-unit-button and remove-sub-unit-button to functions.

add_sub_unit function copies the new-sub-unit-fields div, replacing the text of ‘new-sub-unit’ in the id and name attributes of the input/label elements with an integer.

remove_sub_unit function hides the input field group, and set the ‘_destroy’ hidden field to “1” (‘true’ also works) so that it will not be created when the form is submitted.

Inspiration for this JavaScript goes to Ryan Bates.

Status Check

At this point we have a parent model, Organizations, with nested attributes for children SubUnits. When creating a new Organization, you can add and remove SubUnits. The entire relationship is submitted with a single form post.

Eliminate Duplicates in the DOM

Everyone else does it in model validations, manipulation of the params hash, or some other convoluted way. But I decided to eliminate duplicates before the submission of the parent form. Pretty easy, actually.

When the Create Organization button is clicked, the above JavaScript loops through all the SubUnit form field elements, marking duplicate entries hidden ‘_destroy’ field with ‘true’, thus preventing them from being saved. Could you just remove the elements? Sure.

Inspiration for the JavaScript remove duplicates in an array to Roman Bataev in this post.

Screen Shot 2012-12-21 at 4.38.21 PM

If you watch your logs, you’ll see that all the SubUnits attributes are submitted, but only those marked with the hidden field ‘_destroy’ == ‘false’ will be created.

The astute reader will also notice that the SubUnit model is attempting to validate the uniqueness of the name attribute, scoping it to a non-existant Organization. I admit it would be best to skip this validation, but I will leave that exercise to you, humble reader.

Sample App

What? You wanted a demo app on the GitHubs? You shall be rewarded, post haste.


The Good

  • Submission of a parent Organization with children SubUnits utilizing nested attributes.
  • Duplicate SubUnits are removed.
  • Darn simple, easy to reproduce. Pretty darn unobtrusive, too.
  • No funky helpers to jack some html into a link element.
  • Is it better than Ryan Bates’ solution? No. Just simpler.

The Bad

  • Only works on creating a new Organization. Does not work for editing an Organization with the ability to add additional SubUnits or destroy existing SubUnits. Future post?
  • My JavaScript is not abstracted to make it easily reusable. Fork it, and submit a patch if you so desire.
  • The SubUnit validation should be skipped when creating through an Organization.
  • No fallback if JavaScript is disabled.

Additional Reading


Posted in Rails. Tagged with , .

Small Revelation – FactoryGirl, build_stubbed, associations, and let

Not really a problem, but something I didn’t really look out for because all my tests were passing… and hey, if tests are passing, I sleep well.

TL;DR Dumped let{}, removed FactoryGirl associations, back to before(:all) and instance variable/ pattern = super speedy tests. 
TL;DR.1 And read the docs.


I need to test a fairly complex permission matrix at the model level. To properly test it, I need to build out several complete company structures, with a multitude of users at various authorization levels. And 200+ actual tests.

Being a fan of rspec’s let{}, I decide to use it copiously. 40 lines worth.

In these tests, I don’t really need to have persisted records, so using FactoryGirl.build_stubbed fits the bill here.

let(:company) { FactoryGirl.build_stubbed(:company)}
… an so on, 39 times. 

What I Found

I thought one particular spec was running a little slow. So I converted the to the shiny new FactoryGirl.build_stubbed. No difference in time. So I decided to watch the test.log. Well, I quickly saw what was happening… it was persisting thousands of records.

But wait. I was using FactoryGirl.build_stubbed. Where were all these records coming from?

Problem #1

In most of my FactoryGirl factories, I was using ‘association :area‘, ‘association :store‘, and so on. Didn’t think much about these until yesterday.

Turns out that FactoryGirl will build/create and save those associated factories, even if you are using or FactoryGirl.build_stubbed. Learned something new there. Honestly, I didn’t expect this behavior, but I understand why. Shoulda read the docs.

Now, the easy way around this is to pass into a FactoryGirl.create/build/build_stubbed a nil for the association, if it is not needed. Ala:

FactoryGirl.build_stubbed(:store, company: fg_company, area: nil)

Now it won’t build out the associated area. Alas, I had forgotten just one of these nil associations. And at the worst possible relationship level, the bottom. So every time one factory was built, it create the entire supporting structure above. Thus, every hit to an let(:invoice) builds an entire company structure from one single FactoryGirl.build_stubbed(:invoice) call.

But it get’s worse.

Problem #2

I love the let{}. But to be honest, I never read the docs on it. Well, I did read them yesterday. Relavent line being (emphasis added):

The value will be cached across multiple calls in the same example but not across examples.

Uh-oh. Let{} is like a before(:each). Which is what most specs need. But I don’t, not for this spec. I’m never modifying the AR records, just testing some methods within models, which don’t modify AR instances.

Resulting Big Problem

Ok, not really a problem. But certainly very, very inefficient.

By forgetting to nil an association in a FactoryGirld.build_stubbed, and with let{} recreating an entire company structure, to the database, for every 200+ test. Well, you get the picture. It’s slooooow. 22 seconds worth of slow.


You know I wouldn’t drag you along this far without a solution.

  1. Just remove all the ‘association :model‘ statements from all FactoryGirl definitions. I know they are handy, but I want CONTROL over my factories. And just one small mistake can make a spec run many X-times longer.
  2. Remove the let{} and replace with the good ol’ instance variable/build pattern.
  3. Move all the instance variables into a before(:all).
before(:all) do
  @company = FactoryGirl.build_stubbed(:company)
  @store = FactoryGirl.build_stubbed(:store, company: company)
  … and so on, 38 times.

Note for step #1. It caused me to refactor some other specs as well. This turned out to be a good thing, as I was able to speed up several other specs, and add some clarity to those specs that required building out a company structure.


2.5 seconds. Not too shabby.

After the refactoring for all tests, I dropped ~30 more seconds off the entire suite.


Hope this helps someone else out there improve the speed of their specs too.

Posted in Rails, Ruby. Tagged with , , .