Skip to content

Calling .index on ActiveRecord::Relation calls Sunspot.index instead of Array.index #7074

Tomtomgo opened this Issue Jul 17, 2012 · 4 comments

4 participants


Recently I upgraded to Rails 3.2.6. Before I was able to do, given that tracks is an array fetched for example by doing tracks = Tracks.last(10):

tracks.each do |t|
  t.position = tracks.index(t)

Now Sunspot is being called and I have to explicitly make this an Array:

tracks.each do |t|
  t.position =

Seems weird, I don't know if this is an issue with Rails or Sunspot, but it happened after I updated to Rails 3.2.6.

Ruby on Rails member

It's because of this commit: cd1be1a - it changes priority in method_missing so that class methods are checked before array methods.

kennyj commented Jul 17, 2012

I thinks so. I commited it because of fixing #6635.
In #6635, I thought we should change priority. first: class methods, second: array methods.

Ruby on Rails member

I think the change is ok, Rails has to prioritize class methods.

@Tomtomgo maybe you could use each_with_index instead?

tracks.each_with_index do |t, i|
  t.position = i

You could also call to_a or all when grabbing the tracks:

tracks = Tracks.last(10).to_a



Okay, thanks for checking it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.