Amokrane
2011-Feb-26 22:38 UTC
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 %> <br/><br/> <%= 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! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Amokrane
2011-Feb-26 23:06 UTC
Re: How to manage the form of a model that has two foreign keys
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 On Feb 26, 11:38 pm, Amokrane <amokrane.chen...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 %> > <br/><br/> > <%= 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!-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.