2012-02-06 97 views

回答

6

該gem不會與mongoid和mongodb一起使用,因爲它的構建允許使用活動記錄使用關係數據庫進行標記。

好消息是,這在mongoid中很簡單。只需添加一個以Array_as_taggable_on列出的名稱命名的新Array字段。如果您也有acts_as_taggable,那麼也包含一個通用標記字段。

如果你將會有一個看起來像這樣的模式:

class User < ActiveRecord::Base 
    acts_as_taggable 
    acts_as_taggable_on :skills, :interests 
end 

您將與mongoid建立這樣的:

class User 
    include Mongoid::Document 
    field :tags, type: Array 
    field :skills, type: Array 
    field :interests, type: Array 
end 

然後,當你想保存標籤,讓說你感興趣,你會做以下幾點:

@user.interests << 'computers' 
+0

謝謝,這是有用的,但我怎麼能找到所有屬於一個特定的標籤?或所有用戶屬於任何給定的標籤? 就像:User.tagged_with([「awesome」,「cool」],:any => true)在acts_as_taggable_on? – d34th4ck3r 2012-02-06 20:54:23

+2

User.any_in(tags:[「awesome」,「cool」]) – 2012-02-06 21:33:49

+0

你能告訴我如何寫入表單以將輸入輸入到數組中嗎? – d34th4ck3r 2012-02-07 20:47:13