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
class Req < ActiveRecord::Base
I also have a _form, where i'm referencing this field to display the user's first and last name as follows:
<%= @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.