3

I have a simple Client table (or model) where a Client can have a Parent Client (only one, or none). I modeled it this way:

class Client < ActiveRecord::Base
  belongs_to :parent, :class_name => 'Client', :foreign_key => 'parent_id’
end

That seems to work fine. I have two questions:

  1. Is that the correct why to represent that relationship in an R3 model.
  2. What should the _form.html.erb look like? For example, I want a drop-down box listing all of the possible Clients (and None) as the Parent field.

2 Answers2

1

I found the solution to #2:

<div class="field">
   <%=f.label :parent %><br/>
   <%= collection_select(:client, :parent_id, Client.all, :id, :name, {:include_blank => true} ) %>
</div>
1

Yes it is, number #1 is correct. Sorry, just realised how old this question is!

curv
  • 3,796
  • 4
  • 33
  • 48