Active Storageを使った場合のFactoryBotの記述の仕方

Active Storageを使った場合のFactoryBotの記述の仕方はすこし特殊な記述を書きます。

 

FactoryBot.define do
factory :item do
name {'hoge'}
price {'800'}
explanation {'ああああ'}
category_id {3}
state_id {3}
burden_id {2}
shippingorigin_id {3}
day_id {1}
user_id {2}
association :user

after(:build) {
|item| item.image.attach(io: File.open('public/images/test.png'),
filename: 'test.png', content_type: 'image/png')
}
end
end

 

after(:build) {
|item| item.image.attach(io: File.open('public/images/test.png'), filename: 'test.png', content_type: 'image/png')
 
↑この記述なんですが、記述の上記あるitemに関するものと,item.imgeとを結びつけてくれる記述ものらしいです。

メンターさんありがとうございます✨

あと、今日教わったことで上の記述とは関係はないですが

 

@items = Item.all.order("created_at DESC")

と記述するとitemに関するもすべてを呼び込んでくれる(だった気が💦)ものです。

うろ覚えですみません。

 

 

久しぶりに書いたので今日のYou Tubeも貼っておきます。

よかったら見たて下🤗

 


絶対に着たらいけない服 TOP3

 

全部よく分かります。僕も参考にできるところは取り入れようと思います。

 

最後まで見て頂きありがとうございます。

 

次回もよろしくお願いします。