【问题标题】:Ruby code as attribute from dbaseRuby 代码作为 dbase 的属性
【发布时间】:2011-10-29 00:24:02
【问题描述】:

我正在建立一个项目,我想将一些 rails 代码移动到我的 mysql 数据库中。我想将我在控制器中使用的条件放入数据库中。我已将此表称为过滤器。属性是名称和过滤器,在这种情况下,我将条件设置为一个简单的条件:'status = ?' ,'P'

现在我想从我的控制器中调用这样的条件:

@programs = Program.where(Filter.find_by_name(params[:filter]).filter)

因此,基于 de url 中的过滤器参数,我调用了条件。我无法让它工作,我认为问题是条件作为字符串返回。所以基本上我最终得到:

@programs = Program.where("'status = ?' ,'P'") # including the string quotes

返回错误:

ActiveRecord::StatementInvalid: Mysql2::Error: Operand should contain 1 column(s): SELECT `programs`.* FROM `programs` WHERE ('status = ?' ,'P')

这确实有效:

@programs = Program.where('status = ?' ,'P')

所以我的情况,控制器和模型都很好。有人知道怎么解决吗?

【问题讨论】:

    标签: ruby-on-rails string model controller conditional-statements


    【解决方案1】:

    只需将其存储在数据库中即可:

    status = 'P'
    

    这就变成了:

    @programs = Program.where("status = 'P'")
    

    应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-29
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多