ruby on rails - FactoryGirl - can't write unknown attribute, and undefined method `name' -
i'm trying create photo
factory that's in many 1 association gallery
. , i'm getting 2 errors, depending on how specify association between 2 models.
here's models:
photo
belongs_to :gallery, class_name: "sevengallery::gallery"
gallery
has_many :photos, class_name: "sevengallery::photo", foreign_key: "seven_gallery_gallery_id", dependent: :destroy
and migrations
create_table :seven_gallery_galleries |t| t.string :title t.timestamps null: false end create_table :seven_gallery_photos |t| t.string :caption t.string :image t.references :seven_gallery_gallery, index: true t.timestamps null: false end add_foreign_key :seven_gallery_photos, :seven_gallery_galleries, on_delete: :cascade
now here's factories: gallery:
factorygirl.define factory :gallery, class: 'sevengallery::gallery' title "an event gallery" factory :gallery_with_photos after(:build) |gallery| gallery.photos << factorygirl.create(:photo_one, seven_gallery_gallery_id: gallery) gallery.photos << factorygirl.create(:photo_two, seven_gallery_gallery_id: gallery) gallery.photos << factorygirl.create(:photo_three, seven_gallery_gallery_id: gallery) end end end end
and photo:
factorygirl.define factory :photo, class: "sevengallery::photo" factory :photo_one end factory :photo_two end factory :photo_three end factory :photo_with_gallery gallery end end end
and here's controller spec generates error:
it "changes is_main true in self , false in rest" photo_one = factorygirl.create(:photo_with_gallery) expect(photo_one.gallery).to be_a sevengallery::gallery end
whenever run test error:
failure/error: photo_one = factorygirl.create(:photo_with_gallery) activemodel::missingattributeerror: can't write unknown attribute `gallery_id` # ./spec/controllers/seven_gallery/photos_controller_spec.rb:
when change association part be:
factory :photo_with_gallery association :gallery, factory: gallery end
i error:
failure/error: photo_one = factorygirl.create(:photo_with_gallery) nomethoderror: undefined method `name' :photo_with_gallery:symbol # ./spec/controllers/seven_gallery/photos_controller_spec.rb:8
schema.rb
activerecord::schema.define(version: 20150428105107) create_table "seven_event_events", force: :cascade |t| t.string "title" t.text "description" t.datetime "start_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "seven_gallery_galleries", force: :cascade |t| t.string "title" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "seven_event_event_id" end add_index "seven_gallery_galleries", ["seven_event_event_id"], name: "index_seven_gallery_galleries_on_seven_event_event_id" create_table "seven_gallery_photos", force: :cascade |t| t.string "caption" t.string "image" t.integer "seven_gallery_gallery_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "is_new", default: true t.boolean "is_featured", default: false t.text "description" t.string "alt" t.integer "position", default: 0 t.boolean "is_main", default: false end add_index "seven_gallery_photos", ["seven_gallery_gallery_id"], name: "index_seven_gallery_photos_on_seven_gallery_gallery_id" end
backtrace:
# /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute.rb:138:in `with_value_from_database' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_set.rb:39:in `write_from_user' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/write.rb:74:in `write_attribute_with_type_cast' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/write.rb:56:in `write_attribute' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/dirty.rb:96:in `write_attribute' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods.rb:373:in `[]=' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/belongs_to_association.rb:83:in `replace_keys' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/belongs_to_association.rb:14:in `replace' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/singular_association.rb:17:in `writer' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/builder/association.rb:123:in `gallery=' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:16:in `public_send' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:16:in `block (2 levels) in object' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:15:in `each' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:15:in `block in object' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:14:in `tap' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/attribute_assigner.rb:14:in `object' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/evaluation.rb:12:in `object' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/strategy/create.rb:9:in `result' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:42:in `run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `block in run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/notifications.rb:166:in `instrument' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/factory_girl-4.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method' # ./spec/controllers/seven_gallery/photos_controller_spec.rb:8:in `block (3 levels) in <module:sevengallery>' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:177:in `instance_exec' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:177:in `block in run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:385:in `block in with_around_and_singleton_context_hooks' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:343:in `block in with_around_example_hooks' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:474:in `block in run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:614:in `block in run_around_example_hooks_for' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-rails-3.2.1/lib/rspec/rails/example/controller_example_group.rb:179:in `block (2 levels) in <module:controllerexamplegroup>' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:333:in `instance_exec' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:333:in `instance_exec' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:387:in `execute_with' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:616:in `block (2 levels) in run_around_example_hooks_for' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-rails-3.2.1/lib/rspec/rails/adapters.rb:113:in `block (2 levels) in <module:minitestlifecycleadapter>' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:333:in `instance_exec' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:333:in `instance_exec' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:387:in `execute_with' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:616:in `block (2 levels) in run_around_example_hooks_for' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:247:in `call' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:617:in `run_around_example_hooks_for' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/hooks.rb:474:in `run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:343:in `with_around_example_hooks' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:385:in `with_around_and_singleton_context_hooks' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example.rb:174:in `run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:549:in `block in run_examples' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:545:in `map' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:545:in `run_examples' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:512:in `run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `block in run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `map' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/example_group.rb:513:in `run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:110:in `block (3 levels) in run_specs' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:110:in `map' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:110:in `block (2 levels) in run_specs' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1526:in `with_suite_hooks' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:109:in `block in run_specs' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/reporter.rb:62:in `report' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:108:in `run_specs' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:86:in `run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in `run' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:in `invoke' # /home/rafael/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-core-3.2.3/exe/rspec:4:in `<top (required)>' # /home/rafael/.rbenv/versions/2.2.0/bin/rspec:23:in `load' # /home/rafael/.rbenv/versions/2.2.0/bin/rspec:23:in `<main>'
any ne appreciated. thanks.
to facing same problem. solution include foreign_key
in both sides: has_many
side , belongs_to
side.
Comments
Post a Comment