Forum: Ruby on Rails circular table refs in activerecord

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Michael C. (Guest)
on 2006-02-02 21: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
table:

users:
   Column     |            Type             | Modifiers
---------------+-----------------------------+-----------------
id            | integer                     | not null default
nextval('public.users_id_seq'::text)
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
processors:

default_processors:
   Column       |         Type         | Modifiers
----------------+----------------------+----------
id              | integer              | not null default
nextval('public.default_mts_id_seq'::text)
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 =>
'default_proc_id'
...

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 %>
         <p>
         <%=h default_proc.initials %>
         <%=h default_proc.default_proc_id %>
         </p>
      <% 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 (users.id = 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
--
Michael Darrin Chaney
removed_email_address@domain.invalid
http://www.michaelchaney.com/
Michael C. (Guest)
on 2006-02-03 04:59
(Received via mailing list)
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
--
Michael Darrin Chaney
removed_email_address@domain.invalid
http://www.michaelchaney.com/
This topic is locked and can not be replied to.