How to create a User and an Account in one form with Devise

Most of my apps these days that are pure html with a backend are written in Ruby on Rails and use the Devise engine to handle password and account creation type things.

For this example we will be assume a few things

  • You have rails 4+ and Devise Setup
  • Devise routes are setting the registration controller as your custom controller.
  • models are User and Account
  • User belongs_to Account (so User has an account_id)
  • User responds to set_admin to deal with however admin is setup.

I was reading through the source the other day and saw this block option in the RegistrationsController for devise

class Devise::RegistrationsController

  # POST /resource
  def create
    yield resource if block_given?
    #other devise stuff
    respond_with resource


yield resource if block_given

means that we can pass some code in as a block (which is just an anonymous function) that will receive the resource as a parameter.


In your resources controller you can override like this

class RegistrationsController < Devise::RegistrationsController
  def create
    super {|user| user.create_account }

So with this code, super sends the block up the chain.  So the result is that when we get to the yield resource, the resources receives the message ‘create_account’.

This is great, now we are able to delegate all of our registration logic to devise.  But wait, what if we now want to set the user as admin on the account and make sure that it is all wrapped up in a transaction?  That is easy, just expand the block.

def create
  ActiveRecord::Base.transaction do
    super do |user|

At this point, we have fulfilled the feature that we create the account along with the user and set that user as the admin on the account, so ship the feature!

All you need to know about resque

Sometimes i need to increase the number of workers in a resque worker.  Here is the command


RAILS_ENV=* bundle exec rake resque:work QUEUE=’*’ BACKGROUND=yes &
can replace * with the queue or environment

Ruby/Rails Metaprogramming – Creating a custom alerting module

I recently needed to be able to dispatch some very context specific alerts when some of my classes are saved. However, the alert and who gets it are completely different based on the class and the context of quite a bit of business logic. This could be accomplished is several ways, I will explain these and then show how i accomplished it with metaprogramming and modules.

The most straightforward way to have done this would be to just add an alert method in each class and then implement the logic there. There are several reasons to shy away from this including polluting your model, strong coupling and harder testing. However, I shy away from that because it promotes bad coding. Each time you tack on just one more method to a class it makes it that much easier to do it again. I’m not saying never just add a method, but think about whether it could be it’s own thing first.

The next would be to have a giant class that handle the logic. This would be better because all the logic for alerting in the app would be in one spot. However, the logic would have several branches (i.e is this a comment or an image or a new post?).

Ultimately i created an alertable module that I included in my models.

module Alertable
  extend ActiveSupport::Concern

  included do
    after_save :alert

  def alert


class Comment < ActiveRecord::Base
  include alertable

From the top, ActiveSupport::Concern gives me both class level methods (after_save) and instance methods (def alert). Pretty neat.

The alert method is a bit of metaprogramming that allows me to put the alert logic in it’s own module namespace (aka directory) like this

models/comments.rb #Normal ActiveRecord Model Location
models/alerts/comments.rb #our new Module

Because alertable is include in Comment, when a comment is saved it now does a callback for the method ‘alert’. That is also included from Alertable so after it sees that there are no alert methods defined in the Comment class it calls “Alerts::Comment” Which is located in models/alerts/comments.rb

The comments.rb file under models/alerts will look something like this.

module Alerts
  class Comment
    def initialize(comment)
      @comment = comment
    def call 
      #Do alerting stuff


So now any time that i need to do some alerting in my program, i include Alertable and then i can either implement the alert method on the class i’m including it into or (preferably) create a new Alerts::#{class_name} file.

I think this setup is better than the 2 previously mentioned because everything that is related to alerting is not located in a single area of the application. The alerts are their own thing in their own area of the application. The downside is that we have callbacks for the Comment class that are completely encapsulated in a module. So that could cause trouble for newer programmers to the project or if the alert ever returns falsy it would halt the save of our comment and not be immediately apparent as to why it was failing.

Learning AngularJS as a Ruby on Rails programmer

A really awesome project i am working on has a Ruby on Rails backend and uses AngularJS on the frontend. I have been on it for around 1 month now and have spent a ton of my free time learning angular and wanted to document my journey.

My journey to angular through javascript frameworks have been a little rocky and I was bearish about them until this experience. I have tried several others including backbone and ember, which resulted in me dropping back down to just using javascript and jquery.

I am really liking it so far. My biggest positive is that it has a noticeably smoother frontend experience. Biggest downside, when you introduce angular into a project you dramatically increase the surface area of your project’s skillset needs. Not only will you need a backend programmer, designer and product manager, you will now need someone who knows angular, it’s testing frameworks and javascript compilation. You also have to deal with browser incompatibilities.

As far as learning goes, the biggest aha moment was similar to one i experienced a few years ago with rails when i realized it was just well thought out ruby code. So, AngularJS is just well thought out javascript. All it does it handles data, presenting it and updating it when it is changed. I am currently bullish about AngularJS and will update this from time to time with additional resources.


First Step: Davetron5000’s Angular Rails Guide
Great book going over basics of how to setup angular with rails.

Second Step: Amazing Intro On Youtube
Watch this at 1.5 or 2x speed. It is phenomenal and was the best at ramping up on fundamentals.

Third Step: Code School
Watch the angular videos several times.

Forth Step (i am here 1 month in): Udemy Course
I am halfway through this and it is filling in the gaps.

Right now i’m capable of writing basic angular and reading intermediate angular. I’ll keep this posted as i advance and find more reasources.

Testing Mulitenancy with Minitest and Capybara

Lately I have used an outside in approach to building my web application in ruby on rails.  What that means is that I am driving my application development by building integration tests.  I picked up this technique a year or so ago from thoughtbot.  So far, i have really enjoyed the insight it has given me and how much less code I write than when i was doing things by building models first.

Many of the applications I end up building as a consultant are multi-tenant application.  So there are many customers that have to live in strict or limited scope from each other.  I have built a really simple helper that I have been using and wanted to share it below in my TestCase class description

class ActiveSupport::TestCaseActiveRecord::Migration.check_pending!
  include Warden::Test::Helpers
  fixtures :all

  def as_user(user)
    login_as users(user.to_sym)


It is super simple and I was a little hesitant to put it out there.  All it does is a login the user, execute the block that is passed in and then log the user out.  In a multitenant app though it is really imporant to check your scopes based on the type of user that is logging in (admin, super admin, org admin, user, anonymous, etc).  On top of that it adds to some very understandable tests.  Ozark and nixa are just two random cities in my area.


scenario "Employee cannot visit other organizations tickets" do
  as_user(:ozark_employee) do
    ozark_ticket = organizations(:ozark).tickets.first
    visit ticket_path(ozark_ticket)

    page.text.must_include unauthorized_error

scenario "Can edit own tickets" do
  as_user(:ozark_employee) do
    visit tickets_path
    click_link 'Edit'
    fill_in 'Description', :with => 'Some new description xxx'
    click_button 'Update Ticket'

    page.text.must_include 'successfully updated'
    page.text.must_include 'Some new description xxx'

This one simple helper has really tied in with my multitenant outside in development and sped up my dev time when building out how different types of users should experience an application.

Script to generate fixtures from a database

So I have used minitest for about 8 months now for testing and all in all it turns out that I loved it more than I expected.  I just moved back to an in progress in house project written using Rspec with FactoryGirl and realized I want my minitest and  fixtures back.  So i found a great script that will generate fixturess based on my database seed.


sql = "SELECT * FROM %s"
skip_tables = ["schema_info"]
ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
  i = "000""#{Rails.root}/test/fixtures/#{table_name}.yml", 'w') do |file|
  data = ActiveRecord::Base.connection.select_all(sql % table_name)
    file.write data.inject({}) { |hash, record|
      hash["#{table_name}_#{i.succ!}"] = record

Redirecting To Dynamic Page Using Javascript

So I recently had a consulting opportunity where the client needed to be sent to a dynamic page after a controller action in rails.  That is easy in the normal rails context, however this all had to be done in a javascript content due to it being a javascript based subscription form for sign up.  I ended up with a nice little parlor trick that is attached below.

A little background, this client used recurly to do recurring billing and recurly uses a javascript system to securely handle creditcard data.  On submit, javascript sends recurly the credit card data and they send you back a token representing the data in their system or you get back errors that you can display to the browser.

Once you have the token there are a couple ways you can approach charging it to add a subscription.  The way that I do it is by setting up a subscription/order controller in rails, that then takes care of creating the subscription through an Ajax request. That allows us to catch any errors (i.e. not enough funds) without forcing the user to re-enter any non-credit card data on the site.

Normally after a successful subscription, you will let the form continue to a subscription/registrations controller and that will take care of the setup.

In this app, there were multiple paths that the user could go based on what they order and their progress in the system. So i needed to redirect them dynamically. The way that i did that is on success instead of allowing the form to continue using javascript, i called the function below.

function redirectOrder(url){
    var plan = $('#recurly_plan_code').val();
    var form = $('<form action="' + url + '" method="get">' +
      '<input type="text" name="recurly_plan_code" value="' + plan + '" />' +

Setup Phonegap on Ubuntu 14.04

Worlds succinctest guide to setting up a phone gap project.

  • -> npm install -g phonegap
  • -> npm install -g ripple-emulator
  • CD to the project
  • -> phonegap build browser
  • ripple emulate

Then open a browser


Thats it!  Your cordova version may be different, the important part is the URI, which is the path between / and ?