0

我有两个模型Board,并且Feed与 join :through model 存在多对多关系Subscription

最近我向 中添加了root_id字段Subscription,并且我想在这样做时设置此字段@board.feeds << @feed所以就像@board.feeds << @feed, root_id: 10

根据rails docs,我可以覆盖这些方法,但不确定如何。

def feeds << (??? - how should I setup arguments here? )
  super
  #super creates new Subscription rekord, but how can I access it 
  #to set root_id field?

  #For now let say I accessed it as subscription

  if root_id 
    subscription.root_id = root_id
  else
    subscription.root_id = self.id
  end
  subscription.save
  #return something regular collection << returns
end
4

1 回答 1

0

root_idon是否Subscription代表Boardor的任何方面Feed?你也许可以在回调中做这项工作Subscription

# Subscription

before_save :assign_root_id

def assign_root_id
  self.root_id = feed.user.id # Whatever root is ....
end
于 2013-07-03T15:26:52.620 回答