nginx 및 unicorn을 실행하는 서버에는 두 개의 다른 데이터베이스에 연결하도록 구성된 레일이 있습니다. 가벼운로드에서도 두 번째 레일 데이터베이스에 액세스하는 엔드 포인트에 대한 웹 요청은 다른 요청의 결과를 리턴합니다.여러 데이터베이스에 연결할 때 다른 요청의 결과를 반환하는 레일스.
예를 들어, http://example.com/user/111/address
과 http://example.com/user/222/address
에 대한 동시 호출이있는 경우 양쪽 호출에 대해 사용자 222의 주소가 반환되고 때로는 양쪽 호출에 대해 사용자 111의 주소가 반환됩니다.
주소는 매우 기본적입니다. 내가 놓친 거지 두 번째 DB에 연결하는 단계가 있습니까
class OtherDbActiveRecord < ActiveRecord::Base
self.abstract_class = true
establish_connection "#{Rails.env}_other_db"
# Prevent creation of new records and modification to existing records
def readonly?
return true
end
# Prevent objects from being destroyed
def before_destroy
raise ActiveRecord::ReadOnlyRecord
end
end
class User < OtherDbActiveRecord
has_one :address
end
class Address < OtherDbActiveRecord
belongs_to :user
end
:
class UserController < ApplicationController
before_filter :load_user
def address
address = @user.address
render json: address, status: 200
end
private
def load_user
@user = User.find params[:id]
end
end
모델 User
및 Address
모두 해당 데이터베이스에 연결하는 기본 클래스에서 두 번째 데이터베이스에 액세스하고 상속 ? ActiveRecord가 다른 쿼리의 결과를 반환하게하는 원인은 무엇입니까?
나는 레일을 사용하지 않지만 아마도 루비 코드가 아닙니다. proxy_cache를 활성화 할 수 있습니까? proxy_cache_key에 요청 경로가 포함되지 않았습니까? – pcting