acts -as- taggabon根据上下文查找所有标记

时间:2022-12-03 11:07:19

So after searching for a tagging gem for my rails app I found the amazing acts-as-taggable gem. Installing it and playing around I discovered that it keeps all the Tags inside a tag db which just holds the Tag.name without the context, instead the context is held in the :through relationship db ( taggings ). For most purposes I can see this being perfect. Except with my app I want to be able to offer the user the ability to tag based on an pre-existing tags ( eg not allow them to create their own ) and acts-as-taggable doesn't have the ability to search all tags within one context built in ( eg, if I were to present an auto-completion of the tag db I would have all the tags in my app included which is'nt what I want )

所以,在为我的rails应用寻找一个标记宝石之后,我发现了一个令人惊叹的标记宝石。安装后,我发现它将所有标记保存在标记db中,标记db只包含标记.name而不包含上下文,而上下文保存在:through relationship db (taggings)中。在大多数情况下,我认为这是完美的。除了我的程序,我希望能够提供用户标签的能力基于一个预先存在的标签(如不允许他们创建自己的)和搜索所有acts-as-taggable没有能力标签在一个上下文建成的(例如,如果我现在标签数据库的自动完成我所有的标记在我的应用程序包括这不是我想要的)

The method below is what I just fleshed out to see if it would work ( which it does ) but I wondered if I was missing something with acts-as-taggable. I mean I can't see anywhere that offers this kind method?

下面的方法是我刚刚充实出来的,看看它是否有效(它确实有效),但我想知道我是否遗漏了一些带acts-as-taggable的东西。我的意思是我在任何地方都看不到这种方法?

<% ActsAsTaggableOn::Tagging.find_all_by_context("tags").each do |tagging| %>
  <%= tagging.tag %>
<% end %>

If for instance acts-as-taggable doesn't do this, is this the best way to do this? It feels a little non performant, Would I be better doing a custom SQL query instead of routing through acts-as-taggable?

例如,如果acts-as-taggable不能这样做,这是最好的方法吗?它感觉有点无性能,我最好做一个自定义SQL查询,而不是通过带标记的动作进行路由吗?

If it helps at all heres a tail of my log:

如果有什么用的话,这是我的一根木头:

Started GET "/users" for 127.0.0.1 at 2011-01-04 14:46:20 +0000
Processing by UsersController#index as HTML
SQL (0.5ms)   SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
User Load (0.1ms)  SELECT "users".* FROM "users"
ActsAsTaggableOn::Tagging Load (0.5ms)  SELECT "taggings".* FROM "taggings" WHERE ("taggings"."context" = 'languages')
ActsAsTaggableOn::Tag Load (0.1ms)  SELECT "tags".* FROM "tags" WHERE ("tags"."id" = 2) LIMIT 1
Rendered users/index.html.erb within layouts/application (10.4ms)

2 个解决方案

#1


16  

You could also use a statement like the following:

您还可以使用如下语句:

# Returns all the tags for the specified model/context with a count >= 1
@tags = YourModel.tag_counts_on(**context**)

Add limit and order:

添加限制和秩序:

# Get the top 5 tags by count
@tags = YourModel.tag_counts_on(**context**, :limit => 5, :order => "count desc")

Access the counts with the count attribute of the tags returned from tag_counts_on

使用从tag_counts_on返回的标记的count属性访问计数

tag.count

#2


0  

I believe there is the way: User.tag_counts_on(:tags)

我认为有办法:User.tag_counts_on(:tags)

=> [#<ActsAsTaggableOn::Tag id: 1, name: "foo">,
#<ActsAsTaggableOn::Tag id: 2, name: "bar">,
#<ActsAsTaggableOn::Tag id: 3, name: "sushi">,
#<ActsAsTaggableOn::Tag id: 4, name: "pizza">]

#1


16  

You could also use a statement like the following:

您还可以使用如下语句:

# Returns all the tags for the specified model/context with a count >= 1
@tags = YourModel.tag_counts_on(**context**)

Add limit and order:

添加限制和秩序:

# Get the top 5 tags by count
@tags = YourModel.tag_counts_on(**context**, :limit => 5, :order => "count desc")

Access the counts with the count attribute of the tags returned from tag_counts_on

使用从tag_counts_on返回的标记的count属性访问计数

tag.count

#2


0  

I believe there is the way: User.tag_counts_on(:tags)

我认为有办法:User.tag_counts_on(:tags)

=> [#<ActsAsTaggableOn::Tag id: 1, name: "foo">,
#<ActsAsTaggableOn::Tag id: 2, name: "bar">,
#<ActsAsTaggableOn::Tag id: 3, name: "sushi">,
#<ActsAsTaggableOn::Tag id: 4, name: "pizza">]