mongodb 和 Rails 3 中的 ActAsTaggableOn

发布于 2025-01-02 12:50:41 字数 477 浏览 3 评论 0原文

我想在我的应用程序中添加标记功能。所以,我正在使用acts_as_taggable_on: https://github.com/mbleigh/acts-as- taggable-on

我在 Gemfile 中添加了以下行:

gem 'acts-as-taggable-on', '~> 2.2.2'

当我在用户模型中添加以下行时:

acts_as_taggable_on

它给了我这个错误:

undefined local variable or method `acts_as_taggable_on' for User:Class

请告诉我我做错了什么?

I want to adding tagging facility in my application . so, I am using acts_as_taggable_on : https://github.com/mbleigh/acts-as-taggable-on

I have added following line my Gemfile:

gem 'acts-as-taggable-on', '~> 2.2.2'

and when I add following line in my user model:

acts_as_taggable_on

It gives me this error:

undefined local variable or method `acts_as_taggable_on' for User:Class

Kindly, tell me what am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒的傷心 2025-01-09 12:50:41

该 gem 无法与 mongoid 和 mongodb 一起使用,因为它的构建是为了允许使用使用活动记录的关系数据库进行标记。

好消息是,这在 mongoid 中非常简单。只需添加一个新的数组字段,以您列出的任何内容命名为acts_as_taggable_on。如果您还有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'

That gem isn't going to work with mongoid and mongodb because it is built to allow tagging using a relational database using active record.

The good news is that this is very simple to do in mongoid. Simply add a new Array field named after whatever you would have listed as being acts_as_taggable_on. If you also have acts_as_taggable, include a generic tags field as well.

If you were going to have a model that looked like this:

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

You would build it like this with mongoid:

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

Then when you wanted to save a tag, lets say as an interest you would do the following:

@user.interests << 'computers'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文