class Project < ApplicationRecord
has_one :location, class: 'Project::Location'
accepts_nested_attributes_for :location, allow_destroy: true
end
class Project::Location < ApplicationRecord
belongs_to project
end
factory
FactoryBot.define do
factory :project_location, class: 'Project::Location' do
association :prefecture
association :project
address { '港区芝公園4丁目2−8' }
station { 'JR浜松町駅' }
end
end
request spec
let!(:prefecture) { create(:prefecture) }
let!(:category) { create(:project_category) }
let(:project_params) do
attributes_for(:project, location_attributes: attributes_for(:project_location,
prefecture_id: prefecture.id)
end
post projects_path, params: { project: project_params }
prefecture_idを指定してあげる