assign_attributesとは
assign_attributesとは
Active Recordで用意されている、特定のattributeを変更するためのメソッド。
def update @article.assign_attributes(article_params) if @article.save flash[:notice] = '更新しました' redirect_to edit_admin_article_path(@article.uuid) else render :edit end end
update_attributesなどと違い、DBへの保存は行われないので、別途saveメソッドを使ってあげる必要があります。
ちなみに、updateとupdate_attributesの違いは引数の渡し方が違うのと、update_attributesはバリデーションのチェックがされないそうです。
※参考にしたサイト
まとめてオブジェクトの属性を変更したい時に便利!assign_attributesメソッド - その辺にいるWebエンジニアの備忘録
update_attibutesとassign_attributes違い - Qiita
【Rails】updateメソッドの使い方を徹底解説! | Pikawaka - ピカ1わかりやすいプログラミング用語サイト