Skip to content

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

Merged
rafaelfranca merged 1 commit into
rails:masterfrom
wangjohn:blank_argument_errors_in_arel
Feb 19, 2013
Merged

Raising an error when query methods have blank arguments.#9258
rafaelfranca merged 1 commit into
rails:masterfrom
wangjohn:blank_argument_errors_in_arel

Conversation

@wangjohn

Copy link
Copy Markdown
Contributor

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

tadast commented Feb 12, 2013

Copy link
Copy Markdown
Contributor

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

jeremy commented Feb 19, 2013

Copy link
Copy Markdown
Member

@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

tadast commented Feb 19, 2013

Copy link
Copy Markdown
Contributor

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

@rafaelfranca

Copy link
Copy Markdown
Member

Seems good. Could you add a changelog entry?

@jeremy

jeremy commented Feb 19, 2013

Copy link
Copy Markdown
Member

@tadast 👍

@wangjohn

Copy link
Copy Markdown
Contributor Author

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

tadast commented Feb 19, 2013

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor Author

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

@rafaelfranca

Copy link
Copy Markdown
Member

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

@jeremy

jeremy commented Feb 19, 2013

Copy link
Copy Markdown
Member

Again:

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

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

@tadast

tadast commented Feb 19, 2013

Copy link
Copy Markdown
Contributor

🙊

@wangjohn
wangjohn deleted the blank_argument_errors_in_arel branch February 19, 2013 19:48
@richardkmichael

Copy link
Copy Markdown

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
Copy Markdown
Contributor Author

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants