ruby-on-rails - Rails/Redis/Soulmate - 如何按用户角色限制搜索结果?

标签 ruby-on-rails ruby redis

我已将我的应用程序设置为使用 Soulmate 和 soulmate.js 进行自动完成。它搜索两种模型 - 用户和书籍。但是,我对用户有多个角色——有些是作者,有些是读者。我只希望作者出现在结果中。

有没有办法根据新用户的角色限制在注册时添加到 Redis 的内容?

在 user.rb 中(使用 "if role = "author")不起作用

def load_into_soulmate
    loader = Soulmate::Loader.new("authors")
    loader.add("term" => fullname, "id" => self.id, "data" => {
      "link" => Rails.application.routes.url_helpers.user_path(self)
      }) if self.role = "author"
  end

  def remove_from_soulmate
    loader = Soulmate::Loader.new("authors")
      loader.remove("id" => self.id)
  end

在 pages.js 中

$('#search').soulmate({
    url: '/search/search',
    types: ['books','authors'],
    renderCallback : render,
    selectCallback : select,
    minQueryLength : 2,
    maxResults     : 5
  })

或者,我可以将角色添加到 Redis 存储的内容中,然后告诉 js 只为角色 = "author"的用户提供服务吗?

谢谢!!

最佳答案

我在 user.rb 的 after_save 中用条件解决了它:

after_save :load_into_soulmate, :if => :is_author

def is_author
    if self.role == "author"
      true
    else
      false
    end
  end

关于ruby-on-rails - Rails/Redis/Soulmate - 如何按用户角色限制搜索结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27711332/

相关文章:

ruby-on-rails - 如何将 rails3 与 RedCloth 一起使用。要求 'RedCloth' 不工作

encryption - 我如何解码这个字符串?\xc3\x99\xc3\xa9\xc2\x87-B[x\xc2

ruby-on-rails - 编辑插入新记录

ruby-on-rails - Rails 得到两个日期之间的差异

ruby - 在 Chef 的提供者的 default.rb 中使用 File::read

redis - 什么是 REPL 以及如何/何时发挥作用?

dependency-injection - SignalR & Autofac - Redis 背板横向扩展

ruby-on-rails - Cancancan嵌套资源授权

ruby-on-rails - rails : rubocop disable Class has too many lines error

ruby - 解码base64字符串并写入文件