Circular table refs in activerecord

I cannot find this anywhere, and it is holding up a project. I have a
very simple rails app with three tables, one of which is the users

Column | Type | Modifiers
id | integer | not null default
login | character varying(25) | not null
password | character varying(150) | not null
user_type | character varying(20) | not null
first_name | character varying(30) | not null
last_name | character varying(30) | not null
email_address | character varying(100) | not null
created_on | timestamp without time zone | not null
updated_on | timestamp without time zone | not null

This is used to log in. There are three “user types”, which are admin,
creator, and processor. Here’s the table which links creators and

Column | Type | Modifiers
id | integer | not null default
user_id | integer | not null
initials | character varying(5) | not null
default_proc_id | integer |
“$1” FOREIGN KEY (user_id) REFERENCES users(id)
“$2” FOREIGN KEY (default_proc_id) REFERENCES users(id)

Each creator id is really a login for an office, so there are mulitple
people there creating documents to be processed. That’s what the
initials are for. The idea is this: when a document comes in, it’ll be
coded with the initials, and that’ll be used to determine who, if
anybody, is the default processor.

I cannot figure out how to make activerecord understand this.

class User < ActiveRecord::Base

has_many :default_procs

class DefaultProc < ActiveRecord::Base
belongs_to :user
has_one :default_proc, :class_name => ‘User’, :foreign_key =>

Here’s the problem. When I load a user, I want a list of default procs.

In views/user/show.rhtml
<% if @user.user_type == ‘creator’ %>
<% if @default_procs && @default_procs.size>0 %>
<% for default_proc in @default_procs %>

<%=h default_proc.initials %>
<%=h default_proc.default_proc_id %>

<% end %>

The line with default_proc.default_proc_id works. If I try to get the
login, though:

<%=h default_proc.default_proc.login %>

It blows up:

PGError: ERROR: column users.default_proc_id does not exist
: SELECT * FROM users WHERE (users.default_proc_id = 2) LIMIT 1

In this case, it should be:
SELECT * FROM users WHERE ( = 4) LIMIT 1

The “2” is the “id” from default_procs. What incantations do I need to
pull this off?

Any help is greatly appreciated.


Michael Darrin Chaney
[email protected]

On Thu, Feb 02, 2006 at 01:15:30PM -0600, Michael C. wrote:

class DefaultProc < ActiveRecord::Base
belongs_to :user
has_one :default_proc, :class_name => ‘User’, :foreign_key => ‘default_proc_id’

Easy one - this “has_one” should be “belongs_to”, also.


Michael Darrin Chaney
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs