2013-07-03 101 views
0

我有两种款式BoardFeed与多对多的关系加入:通过型号Subscription凌驾收藏<<方法

最近我加入root_id领域Subscription,我想安装这个领域,当我做@board.feeds << @feed所以它会像@board.feeds << @feed, root_id: 10

根据轨道的文档,我可以重写这些方法,但不知道如何。

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 
+3

覆盖这样的方法很少是一个好主意。我只想定义一个像'def add_feed feed'这样的订阅方法,并且在那里做所有额外的东西,并使用它来代替使用'<<'。 – p1100i

回答

0

是否上Subscriptionroot_id表示BoardFeed的任何方面?你也许可以在Subscription的回调中做这项工作:

# Subscription 

before_save :assign_root_id 

def assign_root_id 
    self.root_id = feed.user.id # Whatever root is .... 
end