【问题标题】:For an instance of ActiveRecord::Relation, is there a method that would return all the conditions/scopes used?对于 ActiveRecord::Relation 的实例,是否有一种方法可以返回所有使用的条件/范围?
【发布时间】:2011-08-22 19:11:37
【问题描述】:

这个问题适用于 Rails 3.x

假设我有一个 Business 模型,Company 和 City 模型...每个 Business 都属于一家公司并属于一个 City...并且我设置了范围,以便:

City.businesses.owned_by(some_company_id)

...返回该城市中由 some_company 拥有的所有企业。

我是否可以在该范围链的末尾使用一种方法来返回用于返回相关活动记录的所有条件?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 activerecord scope


    【解决方案1】:

    通过在ActiveRecord::Relation 上调用arel.constraints 或仅constraints,您可以访问条件数组。

    例如,在我的一个应用程序中,我有一个名为 active 的作用域:

    z = Auction.active.constraints.last
    => #<Arel::Nodes::Grouping:0x007fa66753c108 @expr="auctions.ending_at > '2011-08-22 23:44:11.591438'">
    z.expr
    => "auctions.ending_at > '2011-08-22 23:44:14.295435'"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      • 1970-01-01
      • 2010-12-29
      • 2011-05-09
      • 1970-01-01
      • 2016-11-22
      相关资源
      最近更新 更多