2013-06-07 5 views
2
나는이를 감안할 때

... 왜 이러한 연결을 연결할 수 없습니까?

create_table "competitors", :force => true do |t| 
t.integer "review_id" 
end 

create_table "questions", :force => true do |t| 
    t.integer "product_id" 
end 

create_table "reviews", :force => true do |t| 
    t.integer "product_id" 
end 

class Competitor < ActiveRecord::Base 
    belongs_to :review 
    def product 
    self.review.product 
    end  
    def questions 
    self.review.product.questions 
    end 
end 

class Review < ActiveRecord::Base 
    belongs_to :product 
    def questions 
    self.product.questions 
    end 

end 

class Product < ActiveRecord::Base 
    has_many :questions 
    has_many :reviews 
end 

class Question < ActiveRecord::Base 
    belongs_to :product 
end 

... 그럼 어떻게이 가능하다조차? 즉

> @competitor.questions 
=> [] 

> @competitor 
=> #<Competitor id: 14, review_id: 7, name: "Colonial FirstState", url: "http://firststate.com.au", created_at: "2013-06-07 06:05:37", updated_at: "2013-06-07 06:05:37", logo_file_name: nil, logo_content_type: nil, logo_file_size: nil, logo_updated_at: nil, logo_processing: false, logo: nil> 

> @review = @competitor.review 
=> #<Review id: 7, product_id: 9, client_name: "BHP", url: "http://bhp.com.au", created_at: "2013-06-07 06:05:37", updated_at: "2013-06-07 06:05:37"> 

> @product = @competitor.review.product 
=> #<Product id: 9, name: "Retail Site Search", created_at: "2013-06-07 05:54:31", updated_at: "2013-06-07 05:54:31", description: nil> 

> @competitor.product 
=> #<Product id: 9, name: "Retail Site Search", created_at: "2013-06-07 06:05:37", updated_at: "2013-06-07 06:05:37", description: nil> 

> @product.questions 
=> [] 

> Product.find(9).questions 
    Product Load (1.0ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT 1 [["id", 9]] 
    Question Load (0.3ms) SELECT "questions".* FROM "questions" WHERE "questions"."product_id" = 9 
=> [#<Question id: 13... etc etc ....>] 

, 나는 그것이 ID를 통해 제품을 찾을 때, 나는 제품의 질문을 볼 수 있습니다,하지만 난 체인의 끝에있는 제품을 찾을 때, 나는 그들을 볼 수 없습니다.

+0

이었다에서 '질문'방법을 제거하십시오 모두 지금은 경쟁과 리뷰 모델과, 감사합니다> 일 competitor.review.product.questions –

+0

@ 시도! 하지만 지금 당연히 내가 실패한 방법은 모든 것들이 실패하는 것입니다. http://api.rubyonrails.org/classes/Module.html#method-i-delegate를 사용하여 메소드를 복원 할 수 있는지 확인합니다. 감사! –

+1

질문에 다른 이름을 부여해야 할 수 있습니다 방법 –

답변

관련 문제