title: Rails 5: How to restrict collection creation from has_many association?
body: <p>I need to know how to restrict collection create! method from an specific association.</p>
  
  <p>I have these models:</p>
  
  <pre><code>  class User &lt; ApplicationRecord
       has_many :company_users
       has_many :companies, through: :company_users
    end
    class CompanyUser &lt; ApplicationRecord
       belongs_to :user
       belongs_to :company
       validates :user, :company, presence: true
    end
    class Company &lt; ApplicationRecord   
       has_many :company_users
       has_many :users, through: :company_users
       accepts_nested_attributes_for :users
    end
  </code></pre>
  
  <p>I need to restrict collection create! method like this:</p>
  
  <pre><code>User.first.companies.create! name: "Company", address: "Address" # &lt;-- want to raise an exception
  
  Company.first.users.create! email: "e@mail.com", password: "1234", password_confirmation: "1234" # &lt;-- want NOT to raise an exception
  </code></pre>
  
  <p>Thank you very much!</p>