2016-09-18 59 views
0

我正在使用可以在這裏找到的寶石行爲作爲標籤 - https://github.com/mbleigh/acts-as-taggable-on。 我的應用/模型中沒有Tag或Taggings模型,因爲該功能直接來自Gem。 標籤創建的很好,一切。但是,我希望能夠在創建標籤時將標籤表中的user_id列分配給當前用戶。喜歡的東西:如何在創建標籤時將user_id分配給標籤?

#pseudocode 
before_save { tag.user_id = current_user.id } 

我雖然對第一延伸標籤模型,但我對如何做,在這種情況下,標籤類寶石中定義不知道(我認爲)。我想我的問題是我如何實現before_save或after_save回調,以確保當使用acts-as-taggable-on創建標籤時,tag.user_id會自動分配給current_user.id? 這是我已經嘗試過,但似乎沒有工作。我試圖添加另一個標記模型(不是在寶石模塊中定義的模型):

class Tag < ActsAsTaggableOn::Tag 
    before_create :assign_user_id 

    def assign_user_id 
     self.user_id = current_user.id 
    end 

end 

感謝您的幫助!

回答

0

acts-as-taggable-on提供標籤所有權。 https://github.com/mbleigh/acts-as-taggable-on的示例:

class User < ActiveRecord::Base 
    acts_as_tagger 
end 

class Photo < ActiveRecord::Base 
    acts_as_taggable_on :locations 
end 

@some_user.tag(@some_photo, :with => "paris, normandy", :on => :locations) 
+0

我知道這一點。我正在問一個具體的問題,如何使我的回調。排除所有權。我沒有使用它是有原因的。 – codigomonstruo

相關問題