ruby on rails - How to setup this association in Factory Girl -


first time doing testing/using factory girl , i'm confused. i've got model called partnerships , has has_one relationship model called schools.

partnership model:

class partnership < activerecord::base    has_one :school end 

school model:

class school < activerecord::base    attr_accessible :name    belongs_to :partnership end 

school factory:

factorygirl.define   factory :school     name "elementary school"    end end 

partnership factory:

factorygirl.define   factory :partnership     association :school      factory :partnership_for_school          after(:create) |partnership|             partnership.school factorygirl.build(:school)         end     end     end  end 

in spec file, tried create simple test:

describe "partnerships"      "should able create partnership school"         school_partnership = factorygirl.create(:partnership_for_school)         school_partnership.school_id.should_not be_nil     end    end 

but when run test, keep getting school_id in partnership object nil. thought built school association in partnership factory, i'm going wrong somewhere.

any ideas on went wrong?

update

here stack trace when run test:

partnerships should able create partnership school  failure/error: school_partnership = factorygirl.create(:partnership_for_school)  activerecord::statementinvalid:    pg::undefinedcolumn: error:  column schools.partnership_id not exist    line 1: select  "schools".* "schools"  "schools"."partner...                                                      ^    : select  "schools".* "schools"  "schools"."partnership_id" = 17 limit 1  # ./spec/factories/partnerships.rb:11:in `block (4 levels) in <top (required)>'  # ./spec/requests/partnerships_spec.rb:8:in `block (2 levels) in <top (required)>'  # -e:1:in `<main>' 

update 2

when run rake db:test:prepare, error:

 activerecord::statementinvalid: pg::syntaxerror: error:  zero-length delimited identifier       @ or near """" line 1: drop database if exists ""                             ^ : drop database if exists "" 

here schema partnership , school tables:

activerecord::schema.define(:version => 20140812210757)    create_table "partnerships", :force => true |t|   t.integer  "school_id"   t.datetime "created_at",      :null => false   t.datetime "updated_at",      :null => false  end   create_table "schools", :force => true |t|    t.string   "name"    t.datetime "created_at",          :null => false    t.datetime "updated_at",          :null => false  end  end 


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -