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

Cache order overrides DB order #37

Open
mlarraz opened this issue Apr 8, 2017 · 0 comments
Open

Cache order overrides DB order #37

mlarraz opened this issue Apr 8, 2017 · 0 comments

Comments

@mlarraz
Copy link
Contributor

mlarraz commented Apr 8, 2017

Given a countries table with two rows:

country_id name
1 China
2 America

And a model

class Country < ActiveRecord::Base
  lookup_by :name, cache: true
end

Prior to v0.11, we'd see:

Country.first.name
# => "China"

because this actually executes a db query (which implicitly orders by insertion).

Afterwards, since Country.all hits the cache instead, we see:

Country.first.name
# => "America"

because the cache orders based on the cache key (alphabetically).

This can be overridden by using the (undocumented) :order option:

class Country < ActiveRecord::Base
  lookup_by :name, cache: true, order: :country_id
end

Country.first.name
# => "China"

but this is tedious and arguably should be the default behavior.

@mlarraz mlarraz changed the title Cache order overrides db order Cache order overrides DB order Apr 8, 2017
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

1 participant