Including a module builds model with all empty attributes

7 views
Skip to first unread message

Gavin Hughes

unread,
Jun 1, 2013, 12:07:43 AM6/1/13
to factor...@googlegroups.com
When I include the module below, factory_girl builds my model with empty attributes.  If I leave off the module, no problems.  Any idea why?

User.create(attributes_for(:user))  # =>  this works

create(:user)  # => all empty attributes

My setup:

class User < ActiveRecord::Base
  include Models::Profile::UserExt

  has_profile { |user| { class_name: "#{user.role.capitalize}::Profile" }}

end

module Models
  module Profile
    module UserExt
      extend ActiveSupport::Concern

      included do
        after_initialize :_init_profile
        after_create :create_profile
      end

      module ClassMethods
        def has_profile(&block)
          @profile_association_block = block
        end
      end
      
      def _init_profile
        block = self.class.instance_variable_get :@profile_association_block
        self.class.has_one :profile, block.call(self)
      end
    end
  end
end

FactoryGirl.define do
  factory :user do
    email { Faker::Internet.email }
    role { %w(sponsor student).sample }
  end
end

Reply all
Reply to author
Forward
0 new messages