title: Validation failed Class must exist
body: <p>I have been (hours) trouble with associations in Rails. I found a lot of similar problems, but I couldn't apply for my case:</p>
  
  <p>City's class:</p>
  
  <pre><code>class City &lt; ApplicationRecord
    has_many :users
  end
  </code></pre>
  
  <p>User's class:</p>
  
  <pre><code>class User &lt; ApplicationRecord
    belongs_to :city
  
    validates :name, presence: true, length: { maximum: 80 }
    validates :city_id, presence: true
  end
  </code></pre>
  
  <p>Users Controller:</p>
  
  <pre><code>def create
      Rails.logger.debug user_params.inspect
      @user = User.new(user_params)
      if @user.save!
        flash[:success] = "Works!"
        redirect_to '/index'
      else
        render 'new'
      end
   end
  
  def user_params
    params.require(:user).permit(:name, :citys_id)
  end
  </code></pre>
  
  <p>Users View:</p>
  
  <pre><code>&lt;%= form_for(:user, url: '/user/new') do |f| %&gt;
    &lt;%= render 'shared/error_messages' %&gt;
  
    &lt;%= f.label :name %&gt;
    &lt;%= f.text_field :name %&gt;
  
    &lt;%= f.label :citys_id, "City" %&gt;
    &lt;select name="city"&gt;
      &lt;% @city.all.each do |t| %&gt;
        &lt;option value="&lt;%= t.id %&gt;"&gt;&lt;%= t.city %&gt;&lt;/option&gt;
      &lt;% end %&gt;
    &lt;/select&gt;
  end
  </code></pre>
  
  <p>Migrate:</p>
  
  <pre><code>class CreateUser &lt; ActiveRecord::Migration[5.0]
    def change
      create_table :user do |t|
        t.string :name, limit: 80, null: false
        t.belongs_to :citys, null: false
        t.timestamps
    end
  end
  </code></pre>
  
  <p>Message from console and browser:</p>
  
  <pre><code>ActiveRecord::RecordInvalid (Validation failed: City must exist):
  </code></pre>
  
  <p>Well, the problem is, the attributes from User's model that aren't FK they are accept by User.save method, and the FK attributes like citys_id are not. Then it gives me error message in browser saying that "Validation failed City must exist".</p>
  
  <p>Thanks</p>