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

Model instantiated using master. Can I query associations using read slave? #212

Closed
nate00 opened this issue Nov 26, 2013 · 1 comment
Closed

Comments

@nate00
Copy link

nate00 commented Nov 26, 2013

Hello. Thank you for all your work on this awesome gem. I have a question about using Octopus with replication (with fully_replicated: false).

If I instantiate a User using the master shard, then all queries for associations go to the master, even if I try to force queries to the read slave. Here's an example (in this app, a user has_many questions):

u = User.first        # fetches from master by default
q = Octopus.using(:read_slave) do
  u.questions.to_a    # sends query to master, but I want it to go to the read slave
end

I would like to be able to fetch the questions from the :read_slave. There are a few ways I see to accomplish this. I can avoid using the ORM:

u = User.first
q = Octopus.using(:read_slave) do
  Question.where(user_id: u.id).to_a
end

Or I can reload the user using the read slave:

u = User.first
q = Octopus.using(:read_slave) do
  user_read_only = User.find(u.id)
  user_read_only.questions.to_a
end

The first solution is ugly for complicated queries and the second requires an extra trip to the database. Is there a nicer way that I'm missing, or could I monkey-patch this functionality into Octopus?

@sobrinho
Copy link
Collaborator

sobrinho commented Oct 4, 2015

This is by design, all associations are loaded from the same shard that the parent model has been loaded.

We are pretending to have a way to circumvent that but it's not possible right now.

@sobrinho sobrinho closed this as completed Oct 4, 2015
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

2 participants