Archive for February, 2010

Was getting this error in a Postgres backed Rails app I was building. After much googling, I found someone mention something about having messed up their migration. My migrations were fine, but I dropped, created, and migrated the test database anyway, and the problem disappeared.

Here’s the error I was getting, in case it helps Google direct someone here:

ActiveRecord::StatementInvalid in 'CardsController GET index with user logged in assigns all cards as @cards'
PGError: ERROR:  relation "cards" does not exist
LINE 4:              WHERE a.attrelid = '"cards"'::regclass
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"cards"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

Read Full Post »

At the last Nashville Ruby on Rails Meetup, four of us each took 5 minutes to present some code, or some approach we use. I presented the way I write controllers. It received a positive response, so even though I’m not sure it’s anything out of the ordinary, I’m posting it here.

Some time last fall, I read Ben Johnson’s post about why he is discontinuing his take on resource controller magic. At that time, I was using Resources Controller, and was just gearing up to fix the bugs that had been introduced by its latest update.

In the article, Ben basically argues that this type of plugin reduces clarity of intent and, while they seem to keep things dry, they actually end up confusing things. At first, I balked at the idea, because what I had in mind with a regular controller was the kind of thing that Rails scaffolding spits out, which is neither dry nor elegant. But then I went about refactoring, the results of which you can see below.

The key points are that all permissions enforcement happens within before_filters, and all resource creation happens within three private methods: find_resources, find_resource, and new_resource (names inspired by Resources Controller). This makes it easy to see at a glance what kind of permissions are needed for which methods. It also makes it easy to set up permission defaults so that if you need to add an odd method, it isn’t missed by the permissions check.

require 'lib/modules/controller/paginated_search'

class OrganizationsController < ApplicationController
  include PaginatedSearch::OrganizationSearch

  around_filter :catch_exceptions

  before_filter :require_user
  before_filter :require_org_admin
  before_filter :find_resources, :only => [:index]
  before_filter :new_resource, :only => [:new, :create]
  before_filter :find_resource, :except => [:index, :new, :create]

  def new

  def create
    if @organization.save : redirect_to(organizations_url)
    else render(:edit)

  def update
    if @organization.update_attributes(params[:organization]) : redirect_to(organizations_url)
    else render(:edit)

  def destroy


  def find_resources
    @organizations = paginated_find_organizations(current_user.managed_organizations)

  def find_resource
    @organization = current_user.managed_organizations.find(params[:id])

  def new_resource
    @organization = Organization.new(params[:organization]) do |organization|
      organization.account_manager = current_user
      organization.expires_on = Date.today + 1.year if organization.expires_on.nil?

  def require_org_admin
    not_authorized_redirect() unless current_user.org_admin?

  def catch_exceptions
  rescue ActiveRecord::RecordNotFound
    flash[:notice] = I18n.translate('flash.missing_requested_data')

Read Full Post »