轨道4控制器:如何使用逻辑运算符正确
我有一个位置模式和我的位置控制器中我使用的是geokit宝石搜索附近的地点:轨道4控制器:如何使用逻辑运算符正确
@locations = Location.near(current_user.location, 250)
如何我居然在一个控制器动作区分比如说一个地点是不是is_remote == true
?
含义:@locations
实际上应该输出所有的位置,它们要么is_remote == true
或near(current_user.location, 250)
。
在此先感谢您的每一个答案!如果您需要更多信息,请告诉我。
只需加在一起:
@locations = Location.near(current_user.location, 250) + Location.where(is_remote: true)
万一有些是重复的,你可以uniq
他们:
@locations = (Location.near(current_user.location, 250) + Location.where(is_remote: true)).uniq
或者你甚至可以使用红宝石set union operator而不是+
和uniq
(虽然有些可能会认为它不可读):
@locations = Location.near(current_user.location, 250) | Location.where(is_remote: true)
嗯......那个relly应该可以工作,但对我而言,它不会以某种方式......我检查我的View是否存在'@ locations.exists?'。只要我按照你的建议做了我会得到一个No Method'未定义的方法'存在吗?''我也保证每个部分都能正常工作,所以你的建议解决方案必须有一个问题。 – Gugubaight
'exists?'是一个'ActiveRecord :: Relation'方法,通过添加或联合两个Location作用域,将'@ locations'变成一个普通数组。使用此解决方案,您需要使用Array或Enumerable方法,如'@ locations.any?'或'@locations.size!= 0' – DiegoSalazar
非常感谢您的帮助! :) – Gugubaight
如何获得'is_remote == true'条件的位置? –
'Location.where(is_remote:true)' – Gugubaight