2012-09-17 1 views
4

nginx 및 unicorn을 실행하는 서버에는 두 개의 다른 데이터베이스에 연결하도록 구성된 레일이 있습니다. 가벼운로드에서도 두 번째 레일 데이터베이스에 액세스하는 엔드 포인트에 대한 웹 요청은 다른 요청의 결과를 리턴합니다.여러 데이터베이스에 연결할 때 다른 요청의 결과를 반환하는 레일스.

예를 들어, http://example.com/user/111/addresshttp://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 

모델 UserAddress 모두 해당 데이터베이스에 연결하는 기본 클래스에서 두 번째 데이터베이스에 액세스하고 상속 ? ActiveRecord가 다른 쿼리의 결과를 반환하게하는 원인은 무엇입니까?

+0

나는 레일을 사용하지 않지만 아마도 루비 코드가 아닙니다. proxy_cache를 활성화 할 수 있습니까? proxy_cache_key에 요청 경로가 포함되지 않았습니까? – pcting

답변

0

기타 DbActiveRecord는 단일 개체입니다. 각 루비 클래스는 하나의 객체이며 하나의 객체입니다. establish_connection을 호출하면 해당 연결의 상태를 설정합니다.

각각 고유 한 클래스 이름을 사용하여 중복 클래스를 사용해보십시오.

class User < ActiveRecord::Base 
end 

class UserOther < OtherDbActiveRecord 
end 

class Address < ActiveRecord::Base 
end 

class Address Other < OtherDbActiveRecord 
end 
+0

내 메인 데이터베이스의 클래스/테이블은 다른 데이터베이스와 완전히 다릅니다. – wusher

관련 문제