Ruby on rails Rails/Redis/Soulmate-如何按用户角色限制搜索结果?
我已将我的应用程序设置为使用Soulmate和Soulmate.js进行自动完成。它搜索两个模型——用户和书籍。但是,我有多个用户角色——一些是作者,一些是读者。我只想让作者出现在结果中 有没有办法根据新用户的角色限制他们在注册时向Redis添加的内容 在user.rb中(使用“if role=“author”)不起作用Ruby on rails Rails/Redis/Soulmate-如何按用户角色限制搜索结果?,ruby-on-rails,ruby,redis,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
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仅在role=“author”的位置为用户提供服务吗
谢谢!!我在user.rb的after_save中用一个条件解决了这个问题:
after_save :load_into_soulmate, :if => :is_author
及
只是一个小小的建议…方法名更改为
author?
,您只需要在方法体role==“author”
中使用一行代码,它将返回T或F。。。
def is_author
if self.role == "author"
true
else
false
end
end