{"id":3822,"date":"2022-01-27T13:02:55","date_gmt":"2022-01-27T04:02:55","guid":{"rendered":"https:\/\/suzutukiblog.com\/?p=3822"},"modified":"2022-09-11T20:19:27","modified_gmt":"2022-09-11T11:19:27","slug":"destroy_all","status":"publish","type":"post","link":"https:\/\/suzutukiblog.com\/index.php\/2022\/01\/27\/destroy_all\/","title":{"rendered":"destroy_all\u306e\u4f7f\u3044\u65b9(1\u5bfe\u4ed6\u30e2\u30c7\u30eb\u306e\u4e00\u62ec\u524a\u9664)"},"content":{"rendered":"<h3>destroy_all\u3068\u306f\uff1f<\/h3>\n<p>\u3059\u3067\u306b\u30c6\u30fc\u30d6\u30eb\u306b\u5b58\u5728\u3059\u308b\u30ec\u30b3\u30fc\u30c9\u3092\u4e00\u62ec\u3067\u524a\u9664\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/p>\n<h3>\u30c6\u30fc\u30d6\u30eb\u306e\u72b6\u614b<\/h3>\n<p>users\u30c6\u30fc\u30d6\u30eb(1)\u306btasks\u30c6\u30fc\u30d6\u30eb(\u591a)\u304chas_many\u3067\u7d10\u4ed8\u3044\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>user.rb\u306f\u3053\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/h3>\n<pre># == Schema Information\r\n#\r\n# Table name: users\r\n#\r\n#  id              :bigint           not null, primary key\r\n#  admin           :boolean          default(FALSE)\r\n#  email           :string(255)\r\n#  image           :string(255)\r\n#  name            :string(255)\r\n#  password_digest :string(255)\r\n#  remember_digest :string(255)\r\n#  created_at      :datetime         not null\r\n#  updated_at      :datetime         not null\r\n#<\/pre>\n<pre>class User &lt; ApplicationRecord\r\n  has_many :tasks, dependent: :destroy\r\n  accepts_nested_attributes_for :tasks, allow_destroy: true<\/pre>\n<p>1\u5bfe\u591a\u306e\u5834\u5408\u306f1\u306e\u65b9(\u4f8b\u3068\u3057\u3066user\u3068\u3059\u308b)has_many :tasks, dependent: :destroy\u3092\u8ffd\u52a0\u8a18\u8ff0\u3059\u308b\u3053\u3068\u3067\u3001user\u3092\u6d88\u3057\u305f\u3068\u304d\u306b\u7d10\u4ed8\u3044\u3066\u3044\u308btask\u3082\u524a\u9664\u3055\u308c\u308b\u3088\u3046\u306b\u3067\u304d\u308b\uff01<\/p>\n<h3>sessions_helper.rb\u306f\u3053\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059(\u307b\u307cRails\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3068\u540c\u3058\u3067\u3059)<\/h3>\n<pre>module SessionsHelper\r\n\r\n  # \u8a18\u61b6\u30c8\u30fc\u30af\u30f3cookie\u306b\u5bfe\u5fdc\u3059\u308b\u30e6\u30fc\u30b6\u30fc\u3092\u8fd4\u3059\r\n  def current_user\r\n    if (user_id = session[:user_id])\r\n      @current_user ||= User.find_by(id: user_id)\r\n    elsif (user_id = cookies.signed[:user_id])\r\n      user = User.find_by(id: user_id)\r\n      if user &amp;&amp; user.authenticated?(cookies[:remember_token])\r\n        log_in user\r\n        @current_user = user\r\n      end\r\n    end\r\n  end\r\n\r\n  # \u6e21\u3055\u308c\u305f\u30e6\u30fc\u30b6\u30fc\u304c\u30ed\u30b0\u30a4\u30f3\u6e08\u307f\u30e6\u30fc\u30b6\u30fc\u3067\u3042\u308c\u3070true\u3092\u8fd4\u3059\r\n  def current_user?(user)\r\n    user == current_user\r\n  end\r\n\r\n  # \u8a18\u61b6\u3057\u305fURL (\u3082\u3057\u304f\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u5024) \u306b\u30ea\u30c0\u30a4\u30ec\u30af\u30c8(\u30d5\u30ec\u30f3\u30c9\u30ea\u30fc\u30d5\u30a9\u30ef\u30fc\u30c7\u30a3\u30f3\u30b0)\r\n  def redirect_back_or(default)\r\n    redirect_to(session[:forwarding_url] || default)\r\n    session.delete(:forwarding_url)\r\n  end\r\n\r\nend<\/pre>\n<h3>task.rb\u306f\u3053\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059<\/h3>\n<p><!--more--><\/p>\n<pre># == Schema Information\r\n#\r\n# Table name: tasks\r\n#  id         :bigint           not null, primary key\r\n#  content    :text(65535)\r\n#  done       :boolean\r\n#  priority   :integer\r\n#  title      :string(255)\r\n#  created_at :datetime         not null\r\n#  updated_at :datetime         not null\r\n#  user_id    :bigint\r\n#\r\n# Indexes\r\n#\r\n#  index_tasks_on_user_id                 (user_id)\r\n#  index_tasks_on_user_id_and_created_at  (user_id,created_at)\r\n#\r\n# Foreign Keys\r\n#\r\n#  fk_rails_...  (user_id =&gt; users.id)\r\n#<\/pre>\n<pre>class Task &lt; ApplicationRecord\r\n  belongs_to :user\r\nend<\/pre>\n<h3>routes.rb\u306b\u4ee5\u4e0b\u306e\u8a18\u8ff0\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/h3>\n<pre><strong>resources :tasks do<\/strong>\r\n<strong>  collection do<\/strong>\r\n<strong>    delete 'destroy_all'<\/strong>\r\n<strong>  end<\/strong>\r\n<strong>end<\/strong><\/pre>\n<h3>\u6b21\u306btasks\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u306bdestroy_all\u30a2\u30af\u30b7\u30e7\u30f3\u3068<\/h3>\n<h3>correct_user_destroy_all\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002<\/h3>\n<pre>class TasksController &lt; ApplicationController\r\n before_action :correct_user_destroy_all, only: :destroy_all\r\n# \u4e00\u62ec\u524a\u9664\r\ndef destroy_all\r\n  @tasks.destroy_all\r\n  flash[:danger] = \"\u5168\u3066\u524a\u9664\u3057\u307e\u3057\u305f!\"\r\n  #\u2193\u3053\u3053\u306f\u81ea\u7531\u3067\u3044\u3044\u3067\u3059\r\n  redirect_to request.referrer || root_url\r\nend\r\n\r\nprivate\r\n\r\n#\u73fe\u5728\u306e\u30e6\u30fc\u30b6\u30fc\u304c\u524a\u9664\u5bfe\u8c61\u306etasks\u3092\u4fdd\u6709\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002\r\n  def correct_user_destroy_all\r\n    @tasks = current_user.tasks\r\n    redirect_to root_url if @tasks.nil?\r\n  end\r\nend<\/pre>\n<h3>\u5168\u3066\u524a\u9664\u3059\u308b\u30dc\u30bf\u30f3\u3092\u30d3\u30e5\u30fc\u306b\u4f5c\u6210\u3057\u307e\u3059\u3002<strong>tasks\/show.html.erb\u306b\u4ee5\u4e0b\u306e\u8a18\u8ff0\u3092\u3057\u307e\u3059\u3002<\/strong><\/h3>\n<pre>&lt;%= button_to \"\u5168\u3066\u524a\u9664\", destroy_all_tasks_path, method: :delete %&gt;<\/pre>\n<pre>#\u30d5\u30a9\u30f3\u30c8\u30aa\u30fc\u30b5\u30e0\u3092\u4f7f\u3046\u5834\u5408\r\n &lt;%= link_to(content_tag(:i, '', class: \"fas fa-bomb fa-3x \", style: \"color: rgb(80,80,80);\"), destroy_all_tasks_path,\r\nmethod: :delete, data: { confirm: \"\u5168\u3066\u524a\u9664\u3057\u307e\u3059\u3002\u672c\u5f53\u306b\u524a\u9664\u3057\u307e\u3059\u304b\uff1f\" }) %&gt;<\/pre>\n<p>destroy_all\u30e1\u30bd\u30c3\u30c9\u306f\u30e2\u30c7\u30eb\u3092\u4ecb\u3057\u3066\u524a\u9664\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>destroy_all\u3068\u306f\uff1f \u3059\u3067\u306b\u30c6\u30fc\u30d6\u30eb\u306b\u5b58\u5728\u3059\u308b\u30ec\u30b3\u30fc\u30c9\u3092\u4e00\u62ec\u3067\u524a\u9664\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002 \u30c6\u30fc\u30d6\u30eb\u306e\u72b6\u614b users\u30c6\u30fc\u30d6\u30eb(1)\u306btasks\u30c6\u30fc\u30d6\u30eb(\u591a)\u304chas_many\u3067\u7d10\u4ed8\u3044\u3066\u3044\u307e\u3059\u3002 user.rb\u306f\u3053\u306e\u3088 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[27,13,32],"tags":[],"class_list":["post-3822","post","type-post","status-publish","format-standard","hentry","category-programming-note","category-rails","category-tips"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts\/3822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=3822"}],"version-history":[{"count":10,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts\/3822\/revisions"}],"predecessor-version":[{"id":5608,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts\/3822\/revisions\/5608"}],"wp:attachment":[{"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=3822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=3822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=3822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}