2010-01-12 10 views
9

레일스에서 ​​연관성을 어느 정도로 사용할 수 있는지 궁금합니다. 내가 User.bids 같은 일을하고 약이 멋진 User.businessesProvider.bids 같은 바로 가기하지만를 설정할 수 있어요레일 has_many : through has_many : through

class User < ActiveRecord::Base 
    has_one :provider 
    has_many :businesses, :through => :provider 
end 

class Provider < ActiveRecord::Base 
    has_many :businesses 
    has_many :bids, :through => :businesses 
    belongs_to :user 
end 

class Business < ActiveRecord::Base 
    has_many :bids 
    belongs_to :provider 
end 

class Bid < ActiveRecord::Base 
    belongs_to :business 
end 

: 고려 사항으로 다음을 가지고? 협회를 연상시키는 것이 가능합니까?

답변

5

이것은 완전히 가능하지만 약간의 추가 작업이 필요합니다. nested_has_many plugin와 함께 사용 다음 모델 정의는 그냥 @user.bids

class User < ActiveRecord::Base 
    has_one :provider 
    has_many :businesses, :through => :provider 
    has_many :bids, :through => :businesses 
end 

class Provider < ActiveRecord::Base 
    has_many :businesses 
    has_many :bids, :through => :businesses 
    belongs_to :user 
end 

class Business < ActiveRecord::Base 
    has_many :bids 
    belongs_to :provider 
end 

class Bid < ActiveRecord::Base 
    belongs_to :business 
end 

그러나, 더 많은 작업을하는 입찰에서 사용자를 받고있는 사용자에 속한 모든 입찰을 가져올 수 있습니다.

class User < ActiveRecord::Base 
    has_one :provider 
    has_many :businesses, :through => :provider 

    def bids 
     user_bids = [] 
     businesses.each |business| do 
      user_bids += business.bids 
     end 
     user_bids 
    end 
end 

class Provider < ActiveRecord::Base 
    has_many :businesses 
    has_many :bids, :through => :businesses 
    belongs_to :user 
end 

class Business < ActiveRecord::Base 
    has_many :bids 
    belongs_to :provider 
end 

class Bid < ActiveRecord::Base 
    belongs_to :business 
end 

그런 다음 원하는 결과를해야 user.bids @ 전화, 당신은 또한 입찰을 캐시 ​​당신이 원하는 경우 다른 멋진 물건을 할 수 있습니다 :이 AFAIK 같은 일을 중지 거기에 아무것도

+2

데이터베이스와 레일 응용 프로그램을 늪에 빠뜨릴 수 있기 때문에 가능한 한 중첩하는 방법에주의해야합니다. 즉, 나는 이것을하기 위해 nested_has_many_through를 어떻게 사용하는지에 대한 세부적인 블로그 포스트를 작성했다 : http://kconrails.com/2010/01/28/nesting-has_many-through-relationships-in-ruby-on-rails/ –

2

매우 유용한 것이지만, has_many : through 관계를 통해 has_many를 할 수는 없습니다. 이것은 조인 엔진의 한계입니다.

다른 방법으로는 똑똑한 하위 선택을 사용하거나이 경우 하위 하위 선택을 사용하거나 조인 깊이를 줄이기 위해 의도적으로 테이블을 비정규 화하지 않을 수 있습니다.

예를 들어, 비즈니스는 제공자의 컨텍스트 내에서 정의되기 때문에 어떤 입찰 요소도 간접적으로 제공자에게 할당된다는 것을 의미합니다. 입찰가와 제공자 간의 직접적인 연관성을 구축하면 입찰가를 직접 쿼리하기가 쉽습니다.

0

.

+0

'bids'는 비 정적 데이터이므로 어떻게 캐시 될까요? 캐시를 업데이트하는 방법은 무엇입니까? –

4

레코드를 가져 오려면 사용하지 않는 것이 좋습니다 #delegate? 그것은 당신이 묘사 한 시나리오에서 적어도 훌륭하게 작동합니다. 더 간단 때문에

class User < ActiveRecord::Base 
    has_one :provider 
    delegates :bids, :to => :provider 
end 

class Provider < ActiveRecord::Base 
    has_many :businesses 
    has_many :bids, :through => :businesses 
    belongs_to :user 
end 

class Business < ActiveRecord::Base 
    has_many :bids 
    belongs_to :provider 
end 

class Bid < ActiveRecord::Base 
    belongs_to :business 
end 

내 그다지 겸손하지-생각에 당신은 방법을 단지를 체인 없어야하지만, 당신은 tadman 말한다 당신이 어떤 미친 사용자 정의 SQL로 이동하지 않는 한 성능 향상을 달성 더 이상입니다.

관련 문제