Topic: Question about multiple fields from one table relating to another

I have a table called reqs, which stores requirement information.  In this table, i have a field called user_id, which i use to store the user_id of the user who created the row. Obviously, this points to my users table and i have set up my models as follows.

class User < ActiveRecord::Base
    has_many :reqs

class Req < ActiveRecord::Base
    belongs_to :user

I also have a _form, where i'm referencing this field to display the user's first and last name as follows:
Owner <br>
<%= @req.user.first_name unless @req.user.first_name.nil? %> <%= @req.user.last_name unless @req.user.last_name.nil? %> </p>

All of that works fine.  But now i've added a new column to the reqs table called last_updated_by.  This field also points to the users table. This is basically so that whenever a record is updated by someone else, I can just store the user_id in this field and display it on the screen as well, so i know who last updated the record.  The problem is, i'm not sure how to join the tables by this field, since it is not using the typical rails convention.

Ideally, I'd like to be able to simply add the field to the form and pull back the data like this, but i'm not sure how to do it since the field_name i'm using does not map to a similarly named table.
Last Updated by <br>
<%= @req.last_updated_by.first_name unless @req.last_updated_by.first_name.nil? %> <%= @req.last_updated_by.last_name unless @req.last_updated_by.last_name.nil? %> </p>

Like I said, this is going to be pulling data back from the same users table, but through last_updated_by field instead of user_id in this case.

do i need to set up another association in the models or do i need to do this mapping in the controller somehow? i'm just not sure how to get this to work. any help would be much appreciated.

Re: Question about multiple fields from one table relating to another

I suggest renaming "last_updated_by" to "last_updated_by_id" or something similar that ends in "_id". Keeping all foreign keys ending in "_id" will make sure there's no confusion between the column content and the object it relates to (Integer vs User).

With that you can do this:

class Req < ActiveRecord::Base
    belongs_to :user
    belongs_to :last_updated_by, :foreign_key => 'last_updated_by_id', :class_name => 'User'

You can then use this association just like the "user" association.

Railscasts - Free Ruby on Rails Screencasts