Taking Advantage of Your Rails Model Generator

This is a quick trick I haven’t explored until now (no really, didn’t know it like 5 minute ago).

When you’re calling rails g model to generate your models, you have the option to pass in the field names inline, like this:

1
$ rails g model UserRole user_id:integer role_id:integer extra_permissions:text

The example above has something interesting: you create two columns of type integer: role_id and user_id. What you really want to express, however, is that you want ot hook up two associations in your UserRole model: belongs_to :role and belongs_to :user. After you generate this migration, you may have to do a couple of things by hand:

  • add add_index commands on the migration to create the index on role_id and user_id before running rake db:migrate
  • add belongs_to associations in app/models/user_role.rb

Well, all of this manual typing can be just eliminated if you use this syntax instead:

1
$ rails g model UserRole user:belongs_to role:belongs_to extra_permissions:text

PyRegex Has Been Updated!

Today I have been rolled a new version of PyRegex, with quite a big changeset on the backend. PyRegex users are not expected to see any changes neither in behavior nor UI, but I’d like to cover the changes here in more detail. The basic topics are:

  • Flask as python webapp framework
  • Python 3.4