Tag: 毁灭

Rails:如何覆盖:destroy方法?

我尝试了很多东西来覆盖:destroy方法的行为,但没有任何作用。 我首先使用acts_as_paranoia插件,但它不适用于has_many中的模型:通过关联。 我想覆盖:destroy方法只是为了做这样的事情: def destroy _run_destroy_callbacks { delete } end def delete self.update_attribute(:status => 0) freeze end 也就是说,我只想更新另一个字段(状态为0)而不是破坏记录本身。

has_many通过关联依赖的破坏在谁叫毁灭的条件下

有没有办法在before_destroy钩子中检查哪个对象(类)被称为destroy ? 在下面的例子中,当一个patient被摧毁时,他们的appointments也是如此(这就是我想要的); 但是,如果有任何与该physician相关的appointments ,我不想让physician被销毁。 再次,有没有办法在before_destory回调中进行这样的检查? 如果没有,是否还有其他方法可以根据呼叫的“方向”(即基于谁呼叫)完成此“破坏检查”? class Physician < ActiveRecord::Base has_many :appointments, dependent: :destroy has_many :patients, through: :appointments end class Patient < ActiveRecord::Base has_many :appointments, dependent: :destroy has_many :physicians, through: :appointments end class Appointment < ActiveRecord::Base belongs_to :patient belongs_to :physician before_destroy :ensure_not_referenced_by_anything_important private def ensure_not_referenced_by_anything_important unless patients.empty? errors.add(:base, 'This physician cannot be deleted because […]

Rails – 如何破坏在Devise下创建的用户?

对于我的应用程序,我有用户帐户,人们可以注册。 我使用devisegem进行设置。 我还有一个用户页面,其中列出了注册到该站点的所有用户以及destroy链接。 我希望我的管理员能够删除用户并将其重定向到此用户列表页面。 但是,当我单击链接以销毁特定用户时,它只会重定向到用户配置文件页面,并且不会从数据库中删除该用户。 有人知道为什么吗? **更新:按照建议更新下面的代码,现在可以使用。 users_controller.rb def destroy @user = User.find(params[:id]) @user.destroy if @user.destroy redirect_to root_url, notice: “User deleted.” end end 用户/ index.html.erb USERS DIRECTORY 用户/ _user.html.erb 的routes.rb devise_for :users match ‘users/:id’ => ‘users#destroy’, :via => :delete, :as => :admin_destroy_user match ‘users/:id’ => ‘users#show’, as: :user resources :users