Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scope without lambda ignores condition chain #10864

Closed
mlitwiniuk opened this issue Jun 6, 2013 · 6 comments
Closed

Scope without lambda ignores condition chain #10864

mlitwiniuk opened this issue Jun 6, 2013 · 6 comments

Comments

@mlitwiniuk
Copy link

Consider following models

class Customers < ActiveRecord::Base
  has many :orders
end
class Order < ActiveRecord::Base
  scope :ordered, order('created_at DESC')
  belongs_to :customer
end

Calling following

customer.orders.ordered.to_sql

will result in following sql code:

SELECT "orders".* FROM "orders"   ORDER BY created_at DESC

I consider this important issue, as it's quite easy just to forget using lambda syntax and results are far from expected. Lambda syntax is quite big change in Rails4 and I suspect, that many people will forget about using it. In my opinion exception should be raised in such situations.

This is somehow connected to not yet merged #10528 and issue #10421 - so this ticket just aims to show how serious situation is.

Due to not merged #10528, it's still an issue on branch 4-0-stable.

@mlitwiniuk
Copy link
Author

Our, temporary, solution can be found here: galdomedia/activerecord-deprecated_finders@dabc9bc

@senny
Copy link
Member

senny commented Jun 9, 2013

/cc @jonleighton

@jeremyrichardson88
Copy link

Not sure if this is by design but while upgrading to rails 4 I was adding the lambda or -> (i thought the same) to the scopes but I get this error when using this syntax:

scope :example, -> { |*args| where('attr = ?', (args.first)) }
error: syntax error, unexpected '|' and the ^ points to the * but no error with this...

scope :example, lambda { |*args| where('attr = ?', (args.first)) }

not sure why this happens exactly

@robin850
Copy link
Member

@jeremyrichardson88 : This is Ruby's design. This is more or less the same thing but the syntax is not exactly the same:

-> *args { ... }
# Or
lambda {|*args| ... }

@rafaelfranca
Copy link
Member

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@robin850 robin850 added this to the 4.0.6 milestone May 2, 2014
@robin850 robin850 removed the stale label May 2, 2014
@rafaelfranca
Copy link
Member

This is the downside of using scopes without lambda. With Rails 4.1 this is not possible anymore. On Rails 4.0 we already show the deprecation warning and it can be changed to raise an exception if you configure the deprecation behavior to :raise. That said I'm closing this issue

@rafaelfranca rafaelfranca removed this from the 4.0.7 milestone May 24, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants