康康思维狮身人面像的current_ability问题 [英] Cancan Thinking Sphinx current_ability Questions

查看:130
本文介绍了康康思维狮身人面像的current_ability问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图让康康思思考狮身人面像但遇到了一些问题。

trying to get cancan working with thinking sphinx but running into some issues.

在使用狮身人面像之前,我在公司视图中看到过:

Before using sphinx, I had this in my companies view:

@companies = Company.accessible_by(current_ability)

这使我的用户无法看到其他人的公司...

That prevented my users from seeing anyone else's companies...

安装狮身人面像之后,我最终得到了:

After installing sphinx, I ended up with:

  @companies = Company.accessible_by(current_ability).search(params[:search], :include => :order, :match_mode => :extended ).paginate(:page => params[:page])

现在显示我所有的公司和

Which now displays all my companies and isn't refining per user based on ability.

会不会为cancan设置ts?

It would see ts isn't set up for cancan?

推荐答案

我认为 accessible_by 可能是一个作用域-它是数据库/ SQL驱动的。 Sphinx有其自己的查询界面,因此ActiveRecord范围不适用。

I think it's more that accessible_by is probably a scope - which is Database/SQL-driven. Sphinx has its own query interface, and so ActiveRecord scopes don't apply.

一种低效的解决方法(将所有公司放在首位):

An inefficient workaround (gets all companies first):

company_ids = Company.accessible_by(current_ability).collect &:id
@companies  = Company.search params[:search],
  :include    => :order,
  :match_mode => :extended,
  :page       => params[:page],
  :with       => {:sphinx_internal_id => company_ids}

需要注意的几件事:sphinx_internal_id是索引模型的主键-Sphinx有自己的主键唯一的标识符,名为id,因此有所区别。另外:您不想在搜索集合上调用 paginate -Sphinx总是分页,因此只需传递:page 遍历搜索调用。

A couple of things to note: sphinx_internal_id is the indexed model's primary key - Sphinx has its own unique identifier named id, hence the distinction. Also: You don't want to call paginate on a search collection - Sphinx always paginates, so just pass the :page param through to the search call.

我可以想到两种更好的解决方法-要么都具有与 accessible_by ,并将相关信息作为属性添加到您的索引中-或者,更简单(如果不太理想),只需获取上面代码段的第一行中返回的公司ID,无需加载将每个公司都设置为ActiveRecord对象。

There'd be two better workarounds that I can think of - either have a Sphinx equivalent of accessible_by, with the relevant information added to your indices as attributes - or, simpler if not quite as ideal, just get the company ids returned in the first line of my above snippet without loading up every company as an ActiveRecord object. Both will probably mean bypassing and/or duplicating Cancan's helpers.

尽管...也许这可以解决问题,采用后一种方法:

Although... maybe this would do the trick, taking the latter approach:

sql         = Company.accessible_by(current_ability).select(:id).to_sql
company_ids = Company.connection.select_values sql
@companies  = Company.search params[:search],
  :include    => :order,
  :match_mode => :extended,
  :page       => params[:page],
  :with       => {:sphinx_internal_id => company_ids}

避免加载不必要的Company对象,使用Cancan helper(如果它是/返回范围) ,并且可以与Sphinx / Thinking Sphinx期望的内容整齐地工作。不过我还没有使用Cancan,所以这有点猜测。

Avoids loading unnecessary Company objects, uses the Cancan helper (provided it is/returns a scope), and works neatly with what Sphinx/Thinking Sphinx expects. I've not used Cancan though, so this is a bit of guesswork.

这篇关于康康思维狮身人面像的current_ability问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆