2012-10-16 2 views
2

나는 내 레일 앱에서 트립을 만들고 그 트립에 카테고리를 추가하려고합니다. 카테고리는 내 DB의 Categories 테이블에 저장되며 사용자는 어떤 카테고리가 여행에 적합한 지 선택할 수 있습니다. 따라서 여러 범주의 여행을 사용할 수 있습니다.다른 테이블을 통한 연결 "알 수없는 키 : 통과"(레일즈)

비록 내가이 주제에 대한 RoR 가이드의 도움을 받아 몇 가지 사항을 알아 냈습니다. 이제는 trip_id와 category_id를 보유해야하는 세 번째 테이블 tripcategories가 있습니다. 권리? 그것으로 나는 다음과 같은 모델을 가지고 :

trip.rb :

class Trip < ActiveRecord::Base 
    attr_accessible :description, :title, :user_id, :triplocations_attributes, :photo 
    has_many :triplocations, :dependent => :destroy 

    has_many :tripcategories 
    has_many :categories, :through => :tripcategories 

    accepts_nested_attributes_for :triplocations, allow_destroy: true 
end 

category.rb :

class Category < ActiveRecord::Base 
    attr_accessible :name 
    has_many :tripcategories 
    belongs_to :trip, :through => :tripcategories 
end 

tripcategory.rb : i '를

class Tripcategory < ActiveRecord::Base 
    attr_accessible :category_id, :trip_id 
    belongs_to :trip 
    belongs_to :category 
end 

이 방법을 시도하고 trip.categories를 내 여행 색인에 호출하려고 시도하면 "Unknown key: through"이라고 표시됩니다. 끔찍한 잘못을 저질렀거나 더 큰 그림을 놓치고 있습니까?

미리 감사드립니다.

답변

4
class Category < ActiveRecord::Base 
    attr_accessible :name 
    has_many :tripcategories 
    has_many :trips, :through => :tripcategories 
end 
관련 문제