加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 对于Rails中的关联,write_attribute的等价物是

发布时间:2020-12-17 04:21:26 所属栏目:百科 来源:网络整理
导读:我想覆盖关联的setter,但write_attribute()不起作用 – 可能是因为该方法仅适用于数据库列. 我已经尝试过super(),但这也不起作用(不认为它会…但值得一猜). 如何覆盖setter?这是我想要做的: def parent=(value) # this line needs to be changed write_att
我想覆盖关联的setter,但write_attribute()不起作用 – 可能是因为该方法仅适用于数据库列.

我已经尝试过super(),但这也不起作用(不认为它会…但值得一猜).

如何覆盖setter?这是我想要做的:

def parent=(value)
    # this line needs to be changed
    write_attribute(:parent,value)

    if value.subject.start_with?('Re:')
      self.subject = "#{value.subject}"
    else
      self.subject = "Re: #{value.subject}"
    end

    self.receivers << value.sender
  end

解决方法

对我有用的是以下内容:
def parent=(new_parent)
  # do stuff before setting the new parent...

  association(:parent).writer(new_parent)
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读