title: Login with user and polymorphic stuff (Rails 4)
body: <p>In my Rails application, I have introduced a way to log-in from different user groups.</p>
  
  <p>I have an User model:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
      ....
  
      belongs_to :operator, polymorphic: true 
  
  end
  </code></pre>
  
  <p>and different groups model, for example Admins, Clients, Workers, all with a:</p>
  
  <pre><code>....
  has_one :user, as: :operator
  </code></pre>
  
  <p>Active Record association.</p>
  
  <p>All works great but I have a problem with sign-in of a new user.</p>
  
  <p>Assuming we want to create a new Client.
  In my Clients_controller.rb I have</p>
  
  <pre><code>.
  .
  .
  def create
          @client = Client.new(client_params)
          @user = User.new(user_params)
          if @client.save &amp;&amp; @client.create_user(user_params)
              redirect_to @client
          else
              render 'new'
          end
      end
  
      private
          def user_params
              params.require(:user).permit(:username, :password, :password_confirmation)
          end
  
          def client_params
              params.require(:client).permit(:description)
          end
  end
  </code></pre>
  
  <p>and (for completeness), this is the new.html.erb associated (labels are in Italian:</p>
  
  <pre><code>&lt;div class ="row"&gt;
      &lt;div class="col-md-6 col-md-offet-3 box"&gt;
          &lt;%= form_for @client do |f| %&gt;
              &lt;%= render 'shared/error_messages' %&gt;
              &lt;%= f.label 'Nome Azienda' %&gt;
              &lt;%= f.text_field :description, class: 'form_control' %&gt;
              &lt;%= fields_for @user do |g| %&gt;
                  &lt;%= g.label 'inserire una username' %&gt;
                  &lt;%= g.text_field :username, class: 'form_control' %&gt;
                  &lt;%= g.label 'inserire una password' %&gt;
                  &lt;%= g.password_field :password, class: 'form_control' %&gt;
                  &lt;%= g.label 'ripetere la password' %&gt;
                  &lt;%= g.password_field :password_confirmation, class: 'form_control' %&gt;
              &lt;% end %&gt;
              &lt;%= f.submit "Crea l'account", class: "btn btn-primary" %&gt;
          &lt;% end %&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Main problem is that the condition:</p>
  
  <blockquote>
    <p>if @client.save &amp;&amp; @client.create_user(user_params)</p>
  </blockquote>
  
  <p>is always <code>true</code>, but if the <code>user_params</code> do not satisfy the validation, user is not created (while, when the <code>user_params</code> are ok, the user is correctly created and the one-to-one polymorphic association with client correctly established).</p>
  
  <p>Side problem is that I don't know a way to capture both client-errors and user-error messages in the html interface. Actually this is the shared/_error_messages.html.erb</p>
  
  <pre><code> &lt;% if @client.errors.any? %&gt;
        &lt;div id="error_explanation"&gt;
          &lt;div class="alert alert-danger"&gt;
            The form contains &lt;%= pluralize(@client.errors.count, "error") %&gt;.
          &lt;/div&gt;
          &lt;ul&gt;
          &lt;% @client.errors.full_messages.each do |msg| %&gt;
            &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
          &lt;% end %&gt;
          &lt;/ul&gt;
        &lt;/div&gt;
      &lt;% end %&gt;
  &lt;% if @user.errors.any? %&gt;
    &lt;div id="error_explanation"&gt;
      &lt;div class="alert alert-danger"&gt;
        The form contains &lt;%= pluralize(@user.errors.count, "error") %&gt;.
      &lt;/div&gt;
      &lt;ul&gt;
      &lt;% @user.errors.full_messages.each do |msg| %&gt;
        &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
      &lt;% end %&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>that is rendered into new.html.erb . I know is not so elegant and also it doesn't show all errors at the same time: if there is a problem with client validation <em>and</em> errors in user validation, the user errors related are now showed.</p>