【问题标题】:Rails 4 Override belongs_to setterRails 4覆盖belongs_to setter
【发布时间】:2015-03-10 04:25:21
【问题描述】:

我在覆盖 belongs_to 属性上的设置器时遇到问题。我有以下内容:

class Task < ActiveRecord::Base
belongs_to :parent_task, :class_name => 'Task', :foreign_key => 'parent_task_id'

def parent_task=(value)
  write_attribute(:parent_task, value)
  unless value == nil
    #remove all groups_belonging_to if this has been made into a child task -i.e. if it has a parent
    self.groups_belonging_to = []
  end
  self.save
end

用户模型有很多任务:

class User < ActiveRecord::Base
  has_many :tasks_created_by, class_name: 'Task', foreign_key: 'created_by_id', dependent: :destroy

在我的测试中,我正在创建一个这样的任务:

@child_task = @user.tasks_created_by.create!(name: "Task to Delete", parent_task: @top_parent)

这给出了错误:

ActiveModel::MissingAttributeError: can't write unknown attribute `parent_task`

当我删除覆盖时没有问题,所以我肯定以某种方式做错了覆盖。我在其他地方使用过非常相似的覆盖逻辑,但之前没有通过关系。

【问题讨论】:

    标签: ruby-on-rails overriding setter belongs-to


    【解决方案1】:

    这最好写成回调。您可以使用before_save 回调来检查parent_task,如果已设置,请清除groups_belonging_to:

    class Task < ActiveRecord::Base
      belongs_to :parent_task, class_name: 'Task', foreign_key: 'parent_task_id'
    
      before_save :clear_groups if: :parent_task
    
      def clear_groups
        self.groups_belonging_to = []
      end
    end
    

    【讨论】:

      【解决方案2】:

      在我覆盖 setter 之前,我使用 alias_method 解决了这个问题:

      class Task < ActiveRecord::Base
      
        belongs_to :parent_task, :class_name => 'Task', :foreign_key => 'parent_task_id'
      
        alias_method :set_parent_task, :parent_task=
      
        def parent_task=(value)
          set_parent_task(value)
          unless value == nil
            #remove all groups_belonging_to if this has been made into a child task -i.e. if it has a parent
            self.groups_belonging_to = []
          end
          self.save
        end
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-30
        • 1970-01-01
        • 2015-05-11
        • 2014-10-23
        • 2011-03-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多