Search code examples
ruby-on-railsrubylambdanamed-scope

Is "taken" a keyword in ruby?


Straight to the question. I have a query like this:

@issue_books = current_user.issue_books
@already_issues =  @issue_books.taken(params[:id])

where taken is a named_scope defined as below:

scope :taken, lambda { |book_id| where(returned: false).where(book_id: book_id)  }

Now everytime I run this query:

@issue_books.taken(params[:id]) 

I get an ArgumentError: wrong number of arguments (1 for 0) error.

If I rename taken to something else like taken_books, all seems to work fine.

So my question is: is taken a keyword in ruby? If not can anyone explain this behavior?


Solution

  • It is not a ruby keyword, but it appears to be a method defined on scopes.

    Try this:

    @issue_books.method(:taken).owner
    #=> ActiveRecord::Delegation
    @issue_books.method(:taken).source_location
    #=> (...)/gems/activerecord-3.2.6/lib/active_record/relation/delegation.rb
    

    So the scope taken you have defined is probably overshadowed by a definition in ActiveRecord::Delegate.

    Update: I did some digging, and taken seems to be defined as an alias for limit in Arel::SelectorManager, a dependency of ActiveRecord.