我只是想在设置值时覆盖属性“parent_id”的设置器来做一些其他的事情。
但是 setter 永远不会被调用。(该值可以被怀疑检索,所以它显然是由rails以另一种方式设置的)
如何实现我的目标?
class Category < ActiveRecord::Base
  acts_as_tree
  has_and_belongs_to_many :photos
  def parent_id=(value)
    puts "-----> This is never called"
    write_attribute(:parent_id, value)
  end
  def parent_id
    puts "------> This is called as suspected"
    read_attribute(:parent_id)
  end
end
我是 ruby 和 rails 的新手,我很困惑那些需要我几个小时才能完成简单事情的事情......
提前致谢...