Skip to content

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

Closed
Tomtomgo opened this Issue Jul 17, 2012 · 4 comments

4 participants

@Tomtomgo

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)
end

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

tracks.each do |t|
  t.position = Array.new(tracks).index(t)
end

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.

@pixeltrix
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
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.

@carlosantoniodasilva
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
end

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

tracks = Tracks.last(10).to_a

Thanks!

@Tomtomgo

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.