【问题标题】:how can i chain boolean logic statements with active record?我如何将布尔逻辑语句与活动记录链接起来?
【发布时间】:2011-01-20 18:34:31
【问题描述】:

我会怎么写
(A 或 B) 与 B

在控制器中?

现在我有了这个(a OR b)

@articles = Article.where ("author = ? OR title = ?"), params[:author],params[:title])

或者有没有更优雅的方式来达到同样的效果? (范围??)

【问题讨论】:

    标签: ruby-on-rails activerecord boolean


    【解决方案1】:

    这正是我编写 MetaWhere (http://metautonomo.us/projects/metawhere) 来解决的问题。

    使用 MetaWhere,查询会写成这样:

    @articles = Article.where(:author.eq % params[:author] | :title.eq % params[:title])
    

    或者这个:

    @articles = Article.where({:author => params[:author]} | {:title => params[:title]})
    

    【讨论】:

      猜你喜欢
      • 2014-09-02
      • 2018-08-10
      • 2011-09-30
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多