title: Rails Form Objects with multiple nested resources
body: <p><a href="http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/" rel="noreferrer">7 Patterns to Refactor Fat ActiveRecord Models</a> - here is a great article about different refactoring approaches using PORO. Under the 3rd caption there is a Form Object pattern, which I really liked and already implemented in one of the projects. There is only an example using one nested resource, but I would like to implement this pattern for multiple nested resources. Maybe someone here had already dealt with this? I don't necessarily need any code examples, just the basic idea would be fine.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Consider this example. I have two models.</p>
  
  <pre><code>class Company
    has_many :users
  
    accepts_nested_attributes_for :users
  end
  
  class User
    belongs_to :company
  end
  </code></pre>
  
  <p>In case of one nested user for company using Form Object Pattern I would write the following:</p>
  
  <pre><code>&lt;%= form_for @company_form do |f| %&gt;
    &lt;%= f.text_field :name %&gt;
    &lt;%= f.text_field :user_name %&gt;
    &lt;%= f.submit %&gt;
  &lt;% end %&gt;     
  </code></pre>
  
  <p>Form Object</p>
  
  <pre><code>class CompanyForm
    include Virtus
  
    extend ActiveModel::Naming
    include ActiveModel::Conversion
    include ActiveModel::Validations
  
    attr_accessor :company, :user
  
    def user
      @user ||= company.users.build
    end
  
    def company
      @company ||= Company.new
    end
  
    def submit(params={})
      company.name = params[:name]
      user.name = params[:user_name]
      persist!
    end
  
    private
  
    def persist!
      company.save!
      user.save!
    end
  end 
  </code></pre>
  
  <p>But what if I have a form, where a company with multiple users can be created. The usual approach is to write it like this, using <a href="https://github.com/ryanb/nested_form" rel="noreferrer">nested_form</a>:</p>
  
  <pre><code>&lt;%= nested_form_for @company do |f| %&gt;
    &lt;%= f.text_field :name %&gt;
    &lt;%= fields_for :users, do |user_form| %&gt;
      &lt;%= user.form.text_field :name %&gt;
    &lt;% end %&gt;
    &lt;%= f.link_to_add "Add a user", :users %&gt;
    &lt;%= f.submit %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>What I am asking is how do I implement that Form Object Pattern in this case?</p>