【发布时间】:2013-11-15 18:40:52
【问题描述】:
我正在尝试在 RoR 上构建一个应用程序,该应用程序通过 Mongoid 使用 MongoDB 作为主要对象,但通过 Opinions https://github.com/leehambley/opinions/ 使用 Redis 有一个喜欢和不喜欢的过程。
它有点工作,但是当我在我的对象上运行方法时,我只是收到一个错误“未定义的方法 `like_by'”,我认为这些方法应该是自动生成的。
我的模型看起来像:
class Punchline
include Mongoid::Document
include Opinions::Pollable
opinions :like, :dislike
field :key, type: String
field :text, type: String
field :won, type: Boolean
field :created, type: Time, default: ->{ Time.now }
field :score, type: Integer
index({ key: 1 }, { unique: true, name: "key_index" })
belongs_to :user
embedded_in :joke
end
然后我跑:
user = User.find(session[:userid])
@joke.punchlines.sample.like_by(user);
但是由于未定义的方法错误而失败:(
我是否需要在其他地方初始化意见
/config/initializers/opinions.rb
Opinions.backend = Opinions::RedisBackend.new
Redis.new(:host => 'localhost', :port => 6379)
【问题讨论】:
标签: ruby-on-rails mongodb redis mongoid