1

I have several scoped methods. When I call them in the console, I get the expected results. When I view them, the scopes are not applied. I see "all", regardless of the filtered scope method. Any ideas?

EDIT - I'm using Ransack to filter results, hence the @search.

Also, do you guys know a cleaner way to compare a datetime to 1.year.ago? What I have below works in the console, but I'm sure there's a nicer way to compare.

Thanks in advance.

invoice.rb

  def self.open
    where(closed: false)
  end

  def self.this_year
    current_year = 1.year.ago
    where("invoice_date >= ?", current_year)
  end

invoices_controller:

  def index
    @search = Invoice.this_year.open.search(params[:q])
    @invoices = @search.result.paginate(:page => params[:page], :per_page => 25)
    @count = @invoices.count
    render 'shared/invoices.html.erb'
  end
Katie M
  • 1,111
  • 6
  • 21
  • 31

2 Answers2

0

I'm not sure where search is coming from, but perhaps it removes existing scopes?

The easiest way to determine the issue is probably to dive into the controller method with pry. In your Gemfile:

gem "pry"

Then run bundle install.

And in the controller

def index
  binding.pry
  # ...
end

Try to reload the invoices page, and you will see that the request hangs. Head over to your server logs, and you should see a console-like prompt.

This will allow you to run Invoice.this_year.open.to_sql and then Invoice.this_year.open.search(params[:q]).to_sql to review how scoping changes.

Also, I'm not sure it gets much cleaner than where("invoice_date >= ?", 1.year.ago). Seems pretty declarative to me. What don't you like about the expression?

Ben
  • 2,096
  • 15
  • 9
  • binding.pry was very painful to install on my computers (actually its dependent gems), and I think it is overkilled to install a gem in this situation. As you suggest in the second part of your answer `Invoice.this_year.open.search(params[:q]).to_sql` should be sufficient to debug this case – MrYoshiji Apr 09 '14 at 17:25
  • Hmm, sorry to hear you had installation troubles. It drops in without issue for me. I agree with you generally, but I'd reach for pry in this situation because the code looks reasonable, and I have a feeling `search` is doing something unexpected. In other words, some extended debugging might be in order. – Ben Apr 09 '14 at 17:33
  • I'm using Ransack, hence the `@search` - I tried loading without the @search like this: `@invoices = Invoice.this_year.sorted.paginate(:page => params[:page], :per_page => 25)` but get the same result – Katie M Apr 09 '14 at 18:09
  • Following this Question http://stackoverflow.com/questions/8805426/how-can-i-use-scopes-with-ransack-in-rails-3 It seems that your code should work @KatieM (I googled "Ransack scope") – MrYoshiji Apr 09 '14 at 18:17
0

Well, it turns out that another scope method was causing the issue:

scope :sorted, order("invoice_date DESC")

I removed that scope option from the Ransack call and it worked.

Thanks all.

Katie M
  • 1,111
  • 6
  • 21
  • 31