Rails,多态关联。使用“触摸:真实”进行破坏,无用的步骤

时间:2018-02-05 10:16:56

标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-5 rails-activerecord

我在 Rails 5

我有这样的多态关联:

模型/ enjoy_level.rb

class EnjoyLevel < ApplicationRecord
  belongs_to :enjoyable, polymorphic: true, touch: true, optional: true
end

模型/ mother.rb

class Mother < ApplicationRecord
  has_one :enjoy_level, as: :enjoyable, dependent: :destroy

  accepts_nested_attributes_for :enjoy_level
end

如果我销毁一个mother(具有touch: true选项),它仍然是:

  • 之前销毁enjoy_level;
  • Mother update_at设置为now();
  • 并最终销毁此Mother记录。

第一步和第二步无用,对吧?

我哪里错了?

2 个答案:

答案 0 :(得分:0)

来自http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

  

:touch - 如果为true,将触摸相关对象(   保存此记录时,updated_at / on属性设置为now   或被摧毁。如果指定符号,则将更新该属性   除了属性上的updated_at /之外还有当前时间。

因此,当您要销毁mother时,您首先调用dependent: destroy个依赖项,即enjoy_leveltouch: true个对象,因为它们mother您正在触及import React, { Component } from 'react'; import { Form, Layout } from 'antd'; const { Header, Footer, Sider, Content } = Layout; export default class Login extends Component { render () { return ( <div> <Layout> <Header style={{backgroundColor: '#555555', height: '5vh'}}>header</Header> <Layout> <Content>main content</Content> </Layout> <Footer>footer</Footer> </Layout> </div> ) } } 对象返回{1}}因为它也会在destroy上更新它。 因此,第二个查询在当前场景中可能没用,但它们按计划工作。

答案 1 :(得分:0)

销毁父对象时,可以在ActiveRecord级别禁用触摸:

ActiveRecord::Base.no_touching do
  mother.destroy
end