How to manage the form of a model that has two foreign keys

Hi,

Basically I have two models: User and Godfather. The godfather table
has three columns:

  • user_id (FK --> User)
  • user_godfather_id (FK --> User)
  • description (text)

Inside each model class, I am adding the following associations:

class User < ActiveRecord::Base

  has_many :godfathers # for user_id
  has_many :godfathers, :foreign_key =>

“user_godfather_id”, :class_name => “Godfather”

  accepts_nested_attributes_for :godfathers
end

class Godfather < ActiveRecord::Base
   belongs_to :zz_user_id, :class_name => "User"
   belongs_to :user_godfather_id, :class_name => "User"
end

Now my question is about how to manage the edit form of this nested
attribute relationships.
Here is how my form looks like at the moment (using the
nested_form_for gem):

 <%= nested_form_for @user do |f| %>
            <%= f.fields_for :godfathers do |godfather_form| %>
                # Here I have an ID text field but what I want

instead is
# to provide a username for this godfather.
<%= godfather_form.label :user_godfather_id %>
<%= godfather_form.text_field :user_godfather_id
%>

                <%= godfather_form.label :description %>
                <%= godfather_form.text_field :description %>

                <%= godfather_form.link_to_remove "Remove this

godfather" %>
<% end %>
<%= f.link_to_add “Add a godfather”, :godfathers %>



<%= f.submit “Update Godfathers” %>

So as I said in the comments, my goal is to be able to provide a
username for the godfather instead of an id. That username is a column
in the User table by the way.

Any idea about how I should go about it?

Thanks!

Just wanted to add a small correction for the Godfather model:

class Godfather < ActiveRecord::Base
belongs_to :user
belongs_to :user_godfather, :class_name => “User”
end