1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

has_one association factory_bot accepts_nested_attributes_for request_spec [自分用]

Posted at
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を指定してあげる

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?