【问题标题】:Rails has_scope gem used on nil param用于 nil 参数的 Rails has_scope gem
【发布时间】:2013-11-15 20:48:31
【问题描述】:

在我的应用程序中,我使用的是 has_scope gem。我已经制定了一些范围,他们正在努力在字段中查找字符串。 但现在我将添加一个新范围来查找特定参数为 nil 的所有记录 怎么做 ? 我找不到正确的语法,现在我在模型中有它:

student.rb

scope :connected, -> connected { where([:last_connected_at].nil?)}


student_controller.rb

class EtudiantsController < ApplicationController

  has_scope :connected


  def index          
    @students = apply_scopes(Student).all
  end


 end

application.html.erb

<%= link_to "Student never connected", "/etudiants?connected=false"  %><br />

但它不起作用......

你能帮我吗?

尼古拉斯

【问题讨论】:

    标签: ruby-on-rails scope has-scope


    【解决方案1】:

    在你的 student_controller.rb 中写:

    has_scope :connected, type: :boolean
    

    在你的 application.html.erb 中:

    <%= link_to "Student never connected", "/etudiants?connected=true"  %><br />
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      你可以试试这个吗?

       scope :connected, -> connected { where([:last_connected_at].present?)}
      

      或者

       scope :connected, -> connected { where([:last_connected_at].try?)}
      

      【讨论】:

      • 非常感谢您的回答,但这不是更好:-(。现在?什么都不说,试试?说:“未定义的方法`try?' for [:last_connected_at]:Array”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多