Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object that isn't fetched doesn't pull associations from cache #36

Closed
elado opened this issue May 16, 2013 · 6 comments
Closed

Object that isn't fetched doesn't pull associations from cache #36

elado opened this issue May 16, 2013 · 6 comments

Comments

@elado
Copy link

elado commented May 16, 2013

(Using master branch)

# first time - no cache at all
> Product.first.fetch_main_category
  Product Load (0.2ms)  SELECT products.* FROM products LIMIT 1
  Category Load (0.2ms)  SELECT categories.* FROM categories WHERE categories.id = 1 LIMIT 1
 => #<Category id: 1 name: "x">

# second time, not using #fetch - no cache for fetch_main_category
> Product.first.fetch_main_category
  Product Load (0.3ms)  SELECT products.* FROM products LIMIT 1
  Category Load (0.3ms)  SELECT categories.* FROM categories WHERE categories.id = 1 LIMIT 1
 => #<Category id: 1 name: "x">

# using #fetch - no cache for fetch_main_category
> Product.fetch(1).fetch_main_category
  Product Load (0.3ms)  SELECT products.* FROM products WHERE products.id = 1 LIMIT 1
  Category Load (0.2ms)  SELECT categories.* FROM categories WHERE categories.id IN (1)
 => #<Category id: 1 name: "x">

# using #fetch - everything's cached
> Product.fetch(1).fetch_main_category
 => #<Category id: 1 name: "x">

Product class has

belongs_to :main_category, class_name: 'Category'
cache_has_one :main_category, inverse_name: :products

Expected: even when Product.first is called, the calls of fetch_main_category will get it from DB, put the Category object in cache and next calls of Product.first.fetch_main_category will read it from cache.

@bdotdub
Copy link

bdotdub commented Jun 6, 2013

Bump.

This seems weird to me as well. Are the methods only applied to objects coming back from memcache?

@elado
Copy link
Author

elado commented Jun 6, 2013

I have a temporary workaround

class Product < AR::Base
  ...
  def fetch_main_category
    Category.fetch(self.main_category_id)
  end
end

ugly, but works.

@dylanahsmith
Copy link
Contributor

cache_has_one embeds the associated main_category record in the Product's cache entry. It isn't meant to be a shortcut to Category.fetch(self.main_category_id) here.

Why aren't you just using cache_belongs_to? It will do what you seem to be expecting.

@elado
Copy link
Author

elado commented Jun 7, 2013

It's not documented on the README. Will check, thanks!

@dylanahsmith
Copy link
Contributor

You're right, that is a valid bug in the documentation at least.

@dylanahsmith
Copy link
Contributor

cache_belongs_to has been documented, so this doesn't demonstrate a bug in the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants