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

Raising an error when query methods have blank arguments. #9258

Merged
merged 1 commit into from Feb 19, 2013

Conversation

@wangjohn
Copy link
Contributor

wangjohn commented Feb 12, 2013

Currently, whenever an activerecord query is included with a nil or nil-like object, no argument error is given. For instance:

Posts.limit().all = Posts.all

That relation is completely useless when there are blank-like arguments. For methods which this doesn't make sense for, I'm adding checks which will raise an ArgumentError.

@tadast
Copy link
Contributor

tadast commented Feb 12, 2013

I would disagree. Often times you want this 'null pattern' behaviour when generating parameters dynamically and this change would force you to write unnecessary conditional statements.

@jeremy
Copy link
Member

jeremy commented Feb 19, 2013

@tadast Could you show some code that's affected, before & after? Note that we're checking that any arguments are passed, not the argument isn't null or an empty string.

>> [''].blank?
=> false
@tadast
Copy link
Contributor

tadast commented Feb 19, 2013

Right, I got confused by splat operator again 😊 Sorry for the FUD @wangjohn, @jeremy

@rafaelfranca
Copy link
Member

rafaelfranca commented Feb 19, 2013

Seems good. Could you add a changelog entry?

@jeremy
Copy link
Member

jeremy commented Feb 19, 2013

@tadast 👍

@wangjohn
Copy link
Contributor Author

wangjohn commented Feb 19, 2013

Changelog entry has been added.

rafaelfranca added a commit that referenced this pull request Feb 19, 2013
Raising an error when query methods have blank arguments.
@rafaelfranca rafaelfranca merged commit 8991083 into rails:master Feb 19, 2013
@tadast
Copy link
Contributor

tadast commented Feb 19, 2013

Sorry, I don't want to cause any more trouble, but I'm thinking about it again... What if someone has a method like this:

def fancy_filter(options = {})
  related = []
  query = {}
  if options[:author_name]
    query[:author] = { name: options[:author_name] }
    related << :author
  end
  where(query).references(related)
end

would it not break things? In cases like this people would have to make sure to omit the #references call unless options[:author_name] is present.

@wangjohn
Copy link
Contributor Author

wangjohn commented Feb 19, 2013

So where is handled separately. I only changed the functionality where blank arguments make strictly no sense. Where still returns self.

@rafaelfranca
Copy link
Member

rafaelfranca commented Feb 19, 2013

@wangjohn the @tadast's problem still occurs with references

@jeremy
Copy link
Member

jeremy commented Feb 19, 2013

Again:

>> [[]].blank?
=> false

It'll only break if you do .references(*related), which would splat an empty array.

@tadast
Copy link
Contributor

tadast commented Feb 19, 2013

🙊

@wangjohn wangjohn deleted the wangjohn:blank_argument_errors_in_arel branch Feb 19, 2013
@richardkmichael
Copy link

richardkmichael commented Feb 20, 2013

I'm confused too; sorry!

Given the discussion above, the CHANGELOG.md description is misleading, I think. It reads: "are the arguments blank", not "are there arguments". I'm confused by the examples:

Post.limit()     # => raises error  
Post.include([]) # => raises error

The limit method has not been changed to call check_empty_arguments, so how will .limit() raise?
As mentioned in the discussion above, with .include([]), args.blank? == false, so how will it raise?

Are there tests for this I can read?

@wangjohn
Copy link
Contributor Author

wangjohn commented Feb 20, 2013

Sorry, the CHANGELOG is misleading and there is an error in it. I'm sending in a PR to fix this and add some more documentation and tests #9332.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants
You can’t perform that action at this time.