I'm trying to create a photo
factory that's in a many to one association with gallery
. And I'm getting two errors, depending on how I specify the association between those two models.
Here's the 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 the migrations
create_table :seven_gallery_galleries do |t|
t.string :title
t.timestamps null: false
end
create_table :seven_gallery_photos do |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 my factories: Gallery:
FactoryGirl.define do
factory :gallery, class: 'SevenGallery::Gallery' do
title "an event gallery"
factory :gallery_with_photos do
after(:build) do |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 do
factory :photo, class: "SevenGallery::Photo" do
factory :photo_one do
end
factory :photo_two do
end
factory :photo_three do
end
factory :photo_with_gallery do
gallery
end
end
end
And here's the controller spec that generates the error:
it "changes is_main to true in self and false in rest" do
photo_one = FactoryGirl.create(:photo_with_gallery)
expect(photo_one.gallery).to be_a SevenGallery::Gallery
end
Whenever I run the test I get this 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 I change the association part to be:
factory :photo_with_gallery do
association :gallery, factory: gallery
end
I get this error:
Failure/Error: photo_one = FactoryGirl.create(:photo_with_gallery)
NoMethodError:
undefined method `name' for :photo_with_gallery:Symbol
# ./spec/controllers/seven_gallery/photos_controller_spec.rb:8
Any help would ne appreciated. Thanks.
Aucun commentaire:
Enregistrer un commentaire