I guess middleware should turn on when we write connect_to blog on the 
model in order to switch read and write access, isn't that?
```

*class Article < ApplicationRecord connect_to database: { writing: :main, 
reading: :main_replica } end*
```
There is an Articles controller with defined CRUD with Get and Post HTTP 
verb, here are similar code that I have in articles_controller:
```
def index
   @articles = Article.all
end

def create
    @article = Article.new(article_params)
    @article.save
    redirect_to @article
end

``` 

On Wednesday, April 24, 2019 at 6:00:07 PM UTC+5:45, eileencodes wrote:
>
> Ok after reading this over a few times I've realized that you wrote broken 
> code on purpose to prove the switching was working.
>
> Did you turn on the middleware that will automatically swap based on HTTP 
> verb? If you didn't your POST/GET requests won't switch connections 
> automatically.
>
> If you're in a model/controller/test you need to wrap it in a 
> `connected_to` block like this
>
> ```
> ActiveRecord::Base.connected_to(role: :reading) do
>   # read from your replica
> end
> ```
>
> Rails can't know if your replica is up to date so we didn't implement 
> always select from the replica, you have to tell it you want that.
>
> On Wednesday, April 24, 2019 at 8:04:45 AM UTC-4, eileencodes wrote:
>>
>> Hey!
>>
>> I've implemented multiple databases in Rails 6, but I didn't implement 
>> multiple *writers*. Rails doesn't know what your replica configuration 
>> is going to be like so for purposes of local development you should use the 
>> same database name for your primary and replica, and then have different 
>> user names. In production you should also use the same name (they are the 
>> same data you're accessing) with different usernames (one set to readonly) 
>> but they shouldn't be in the same place.
>>
>> Hope that helps,
>> Eileen
>>
>>>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to