Forum: Ruby on Rails circular table refs in activerecord

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
5b9884f1eabc8587f2c9dfd2b1ccf9a6?d=identicon&s=25 Michael Chaney (Guest)
on 2006-02-02 20:17
(Received via mailing list)
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
5b9884f1eabc8587f2c9dfd2b1ccf9a6?d=identicon&s=25 Michael Chaney (Guest)
on 2006-02-03 03:59
(Received via mailing list)
On Thu, Feb 02, 2006 at 01:15:30PM -0600, Michael Chaney 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
This topic is locked and can not be replied to.