简体   繁体   English

使用 rails scopes gem 限定整数值的问题

[英]Problems scoping integer values with rails scopes gem

I have a model CoffeeShops which includes an integer value wifi_restrictions.我有一个模型 CoffeeShops,其中包含一个整数值 wifi_restrictions。

The integer value of this field will represent the number of hours you can use the wifi.此字段的整数值将表示您可以使用 wifi 的小时数。

I'm trying to set up scopes for this so that I can search我正在尝试为此设置范围以便我可以搜索

CoffeeShop.has_wifi_restrictions

...and it will return all coffee shops with a wifi_restrictions value which is greater than 0. ...它将返回所有 wifi_restrictions 值大于 0 的咖啡店。

I'm using the rails has_scope gem: https://github.com/plataformatec/has_scope我正在使用 rails has_scope gem: https : //github.com/plataformatec/has_scope


I have tried every variation I can think of to implement the scope in the model, but the syntax of this block is making my head spin.我已经尝试了我能想到的所有变体来实现模型中的范围,但是这个块的语法让我头晕目眩。

I have also tried both我也试过

has_scope :has_wifi_restrictions, type: :boolean

as well as

has_scope :has_wifi_restrictions, type: :integer

I'm not sure what this should be.我不确定这应该是什么。 A side question here is whether the scope block in the model essentially turns the integer value into a boolean value under the new scope method.这里的一个附带问题是模型中的 scope 块是否在新的 scope 方法下本质上将整数值转换为布尔值。

ie IE

my_coffee_shop.wifi_restrictions = 1

converts into转换为

my_coffee_shop.has_wifi_restrictions = true

I'm not sure exactly how that works or how to properly implement it.我不确定它到底是如何工作的或如何正确实施它。


I know that in my model I need something like the following:我知道在我的模型中我需要如下内容:

class CoffeeShop < ApplicationRecord
          scope :has_wifi_restrictions, ->(hours) { where(wifi_restrictions: hours.positive?) }
end

And in the controller I need something like:在控制器中,我需要类似的东西:

class CoffeeShopsController < ApplicationController
  has_scope :has_wifi_restrictions, type: :boolean
end

When I try to do the search当我尝试进行搜索时

CoffeeShop.has_wifi_restrictions

I get the following:我得到以下信息:

ArgumentError: wrong number of arguments (given 0, expected 1)

- ——

I appreciate there's a number of questions here all bundled together, but I would be grateful for both solutions, as well as general advice in understanding how to use scope.我很感激这里有很多问题都捆绑在一起,但我会很感激这两个解决方案,以及理解如何使用范围的一般建议。

The has_wifi_restrictions scope that you defined required that you pass in the number of hours.您定义的has_wifi_restrictions范围要求您传入小时数。 That does not seem to match the goal you stated (all coffee shops that have wifi restrictions).这似乎与您所说的目标不符(所有有 wifi 限制的咖啡店)。 The latter would be something like后者将类似于

class CoffeeShop < ApplicationRecord
  scope :has_wifi_restrictions, -> { where('hours > 0') }
end

Defining a scope is fundamentally no different than defining a query (and has nothing to do with the has_scope gem).定义范围与定义查询从根本上没有什么不同(并且与 has_scope gem 无关)。

To use that scope with the has_scope gem, you'd define a boolean scope in your controller:要在 has_scope gem 中使用该范围,您需要在控制器中定义一个布尔范围:

class CoffeeShopsController << ApplicationController
  has_scope :has_wifi_restrictions, type: :boolean
end

With that, the has_wifi_restrictions scope will be applied to CoffeeShop only when a 'has_wifi_restrictions=true' is part of the query string routed to the CoffeeShopsController.这样,只有当“has_wifi_restrictions=true”是路由到 CoffeeShopsController 的查询字符串的一部分时,has_wifi_restrictions 范围才会应用于 CoffeeShop。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM