0

Currently I am upgrading my application to rails 4.i know there are some syntax changes made for writing scopes in rails4 for SQL statement. below is the scope in rails 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)

how to rewrite this in rails 4

1
  • 1
    You have error in your scope. It should be placed in lambda - otherwise your dates are evaluated once, at class code execution. This addresses to Rails 3 also. Commented Nov 26, 2013 at 9:45

1 Answer 1

1

I guess what ever you have written should work but in rails 4 you should write like

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) }

Check the documentation http://api.rubyonrails.org/classes/ActiveRecord/Scoping/Named/ClassMethods.html#method-i-scope

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.