reset association cache for polymorphism

Hi,

I have a product class with category and a polymorphic attributes details_info, that depending on category go to one or another table to get the detail

Class Product

category

details_info

end

Class DetailsInfo

end

Class Pencil

include DetailsInfo

type

color

end

Class Notebook

include DetailsInfo
size

end

and to display products i do

prds = Products.all

prds.each do |pr|

if pr.category == ‘pencil’

DetailsInfo.table_name = ‘pencil’

else if pr.category == ‘notebook’

DetailsInfo.table_name = ‘notebook’

end

end

(this is just kind of algo I use)

All this was working fine but with rails > 4.2 , it doesnt work anymore.

The first time pr.details_info will be called, it seems that the table name is cached somewhere, and then all other products are using the same table_name. When I check during loop, I can see that DetailsInfo.table_name is always correct, but still when pr.details_info will be called , it used the first table_name that was used. I can also see that the method table_name is called every step of the loop, and it return the good table_name, but still, the sql request search on bad table.

How can I reset that ? Ive tried a lot a thing like emptying association_cache, also different things with reflect_on_all_associations, reset_column_information, reload, …

thanks

FWIW, you should take a look at the “Polymorphic Associations” section in the Guides: http://guides.rubyonrails.org/association_basics.html#polymorphic-associations My guess is that that functionality will allow you to avoid the `table_name` swapping that’s tripping up the code above.

But also note that this list is for development of the Rails framework, not for debugging individual applications. rails-talk is the correct place for this question.

—Matt Jones