Rails gem rails3-jquery-autocomplete如何按用户划分范围 [英] Rails gem rails3-jquery-autocomplete how to scope by user
问题描述
我正在使用Rails gem rails3-jquery-autocomplete向帖子添加类别.
I'm using the Rails gem rails3-jquery-autocomplete to add categories to posts.
我想将搜索限制为在结果中仅包含属于当前用户或帖子作者的类别.
I would like to restrict the search to include only categories that belong to the current user or post's author in the results.
文档说我可以指定范围:
The documentation says that I can specify a scope:
:范围
添加了使用范围的选项.在数组中传递作用域.例如:scopes => [:scope1,:scope2]
Added option to use scopes. Pass scopes in an array. e.g :scopes => [:scope1, :scope2]
但是我不确定如何在此处传递用户ID?
But I'm not sure how I would pass the user id here?
这似乎是一种常见的情况,我是否缺少明显的东西?
It seems like a comon scenario, am I missing something obvious?
我找到了一个建议修改get_item
方法的答案,但这似乎破坏了自动完成功能
I found an answer that suggests modifying the get_item
method, but that seems to break the auto-complete
推荐答案
在posts_controller
中:
def get_autocomplete_items(parameters)
items = super(parameters)
items = items.where(:user_id => current_user.id)
end
我首先调用原始的get_autocomplete_items方法,然后通过current_user.id过滤出结果.
I'm first calling the original get_autocomplete_items method, and then filtering out the results by current_user.id.
此问题有助于: 轨道3:alias_method_chain仍在使用吗?
这篇关于Rails gem rails3-jquery-autocomplete如何按用户划分范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!