如何为SQL语句编写Rails 4作用域?

时间:2013-11-26 09:41:19

标签: ruby-on-rails ruby-on-rails-4 upgrade

目前我正在将我的应用程序升级到rails 4.i知道在rails4中为SQL语句编写范围有一些语法更改。 以下是第3栏中的范围:

scope :current_active_leases,     group("leases.id").where("leases.commencement <= ? and (leases.expiration >= ? or leases.mtm=?) and leases.is_executed= ? ", Time.now.strftime("%Y-%m-%d"), Time.now.strftime("%Y-%m-%d"), true, true)

如何在rails 4中重写它

1 个答案:

答案 0 :(得分:1)

我猜你写的东西应该有用但是在rails 4中你应该像

一样写
scope :current_active_leases,  -> {group("leases.id").where("leases.commencement <= ? and (leases.expiration >= ? or leases.mtm=?) and leases.is_executed= ? ", Time.now.strftime("%Y-%m-%d"), Time.now.strftime("%Y-%m-%d"), true, true) }

查看文档 http://api.rubyonrails.org/classes/ActiveRecord/Scoping/Named/ClassMethods.html#method-i-scope