문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top