My question is about object.collections (AssociationProxy).
We all know that it pretends to be an Array, but in reality it’s
actually an AssociationProxy.
But, Why does it have its own db intensive include? method?
@product = Product.find 1
(product habtm categories)
We have 1000 categories in the system.
In the product edit admin, we display a list of checkboxes including all
categories, and the ones that are associated to the product are checked.
<% Category.all do |category| %>
<%= check_box_tag “product[category_ids]”, category.id,
<% end %>
but it triggers 1000 sql queries like this (very time consuming):
categories INNER JOIN
categories_products.category_id WHERE (
id = 1) AND
categories_products.product_id = 1 ) LIMIT 1
Why doesn’t it use the include? method of the Array? (way faster and
doesn’t need db queries at all, since the association is already
loaded.) Why does it need its own include? method?
If I change my code to this it skips those unnecessary db queries, and
return the result lightning fast:
what do you guys think?