Error using FactoryGirl with has_secure_password in feature spec
<p>Also using mongoid if that's relevant.</p>

<p>When I create a user with factory girl, it says the user is valid. However, when I access that user via ActiveRecord, is says it's invalid. Here's an overview of the issue:</p>

<pre><code>user = create(:user, :activated)
user.valid? # =&gt; true
User.count # =&gt; 1
db_user = User.first
db_user == user # =&gt; true
db_user.valid? # =&gt; false
db_user.errors.count # =&gt; 0
# it only shows the error messages once I try to modify an attribute
db_user.email = "user@example.com"
db_user.save # =&gt; false
db_user.errors # =&gt; @messages={:password=&gt;["is too short (minimum is 6 characters)", "can't be blank"]}
</code></pre>

<p>Here's my factory:</p>

<pre><code>FactoryGirl.define do
  factory :user do
    name { Faker::Name.name }
    email { "#{name.gsub(/[^0-9a-zA-Z]/i, '')}@example.com" }
    phone { Faker::Base.numerify("#{"#" * 10}") }
    admin false
    password "password"
    password_confirmation "password"
  end
end
</code></pre>

<p>Here's the relevant parts of the User model:</p>

<pre><code>class User
  include Mongoid::Document
  include ActiveModel::SecurePassword

  field :name,              type: String
  field :email,             type: String
  validates :password, length: { minimum: 6 },
                      presence: true
  has_secure_password
</code></pre>