2012-12-26 2 views
2

나는 열망하는로드를 통해 가능한 한 DB 호출을 거의하지 않겠다는 몇 가지 시나리오를 가지고 있지만 잘 수행하지 못했습니다.mongoid와 함께 RABL을 사용할 때 DB 호출 줄이기

아래의 두 가지 시나리오를 감안할 때 가능한 한 적은 수의 호출로 RABL을 어떻게 변경할 수 있습니까?


개체 모델 :

Posts 
-> belongs_to user 
-> has_many: Comments 
    -> Comment belongs_to user 
-> has_many: Tags 
    -> Tag belongs_to user 

RABL

node(:comments) do |p| 
    p.filtered_comments(@user) 
end 

child :tags do 
    attribute :text 
    child :users do 
    attribute :nickname 
    end 
end 

CONTROLLER 쿼리를

Post.includes(user, comments, tags)... 

POST를 (이 두 가지 많은 개별 호출을 수행 할 DB의 원인이됩니다). RB

def filtered_comments 
    comments = self.comments.where(:blocked=>false).all 
    json = Rabl::Renderer.json(comments, 'comments/list', view_path: 'app/views') 
    JSON.parse(json).map do |c| 
     c['comment'] 
    end 
end 
+0

얼마나 많은 검색어가 발생했는지에 대한 로그가 있습니까? –

+0

사용자가 'filtered_comments' 메소드를 받아들이지 않으면 어떻게 전달합니까? –

답변

1

일반적으로 컨트롤러는 반복되는 객체를 정의합니다 (예 : @user).

컨트롤러에서 나는 일반적으로 @user = User.find(1).includes(:permissions, :articles)과 같은 사용 권한 및 아티클과 같은 관계를 열렬히로드합니다.이 사용자 개체의 응답은 respond_with @user입니다.

# some_file.json.rabl 
object @user 
child :permissions do 
    attributes :name 
end 
node :first_article do |u| 
    u.articles.first 
end 

이가 수다보기 파일의 내 버전을 고정 :

그런 다음 rabl 파일에서, 내가 좋아하는 뭔가가있다.

관련 문제