Help with a self-regulatory model and view in Rails 3

I have a simple client table (or model) where the Client can have a parent client (only one or none). I modeled it like this:

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

It seems to work. I have two questions:

  • Is it right to represent this relation in the R3 model.
  • What should _form.html.erb look like? For example, I want the drop-down list to list all possible Clients (and No) as the Parent field.
+3
source share
2 answers

I found a solution for # 2:

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

, # 1 . , , !

+1

Source: https://habr.com/ru/post/1779014/