Nested model and

I am new to RSpec and BDD, learning it while starting a new project.

I am writing model tests for a user model with a has_one association:

class User < ActiveRecord::Base

has_one :core_profile
validates_associated :core_profile, :as => :core_profileable
accepts_nested_attributes_for : core_profile

after_initialize :build_associations

def build_associations
build_core_profile unless core_profile


class CoreProfile < ActiveRecord::Base

attr_accessor :password, :password_confirmation
attr_accessible :user_name
attr_protected :hashed_password
validates :user_name, :presence => true
validates :first_name, :presence => true
validates :last_name, :presence => true

belongs_to :core_profileable, :polymorphic => true


I am using Factory Girl to test model data.
I have been trying to test creation of a User. Testing for a valid
User and valid CoreProfile work fine, also invalid Parent and valid
CoreProfile… however when I test for valid User and invalid
CoreProfile I get a failure due to “Validation failed” - it seems to not
return invalid for the User… here is the test in question:

it “should be invalid :invalid_core_profile_user” do
user =
user.core_profile.should_be invalid?

and the Factory definitions:

Factory.define :invalid_core_profile_parent, :class => User do |u| ‘[email protected]
u.phone_number ‘(925) 555-1212’
u.association :invalid_core_profile

Factory.define :invalid_core_profile, :class => CoreProfile do |cp|
cp.first_name ‘Barry’
cp.last_name ‘White’
cp.user_name ‘’
cp.password ‘password’
cp.password_confirmation ‘’

I am assuming that creates the
associated objects first but does not bubble up the validity, just
breaks out with the “Validation failed” message.

Maybe I am doing this wrong - should User only handle its own
validation and not its associated models? Thing is - the CoreProfile
should only be saved as part of a User, so its important to test them

I have googled high and low and can’t seem to find an answer.

Thanks for reading!