2

주제에 하위 주제가있는 데이터 구조가 있는데, 하위 주제는 다시 원래 주제에서 약 6 단계로 내려갑니다. 각 주제에는 여러 하위 주제가 있습니다. 이 데이터를 트래버스하고 각각의 하위 항목에 속한 데이터를 가져 오는 방법을 찾고 있습니다 ... "다운 스트림"을 원하는 데이터를 가져 오는 것 같이 말입니다. 레일즈 애플리케이션의 복잡한 데이터 구조 탐색

For example Given a topic structure: 
Harry_Potter > Characters > Slitherin_House.videos 

내가 Slitherin_House, 문자 및 Harry_Potter에 대한 비디오 목록에 표시 멤버 각각에 대한 비디오를 원하는 ((즉 slitherin 집 가정은 회원, 말포이, 크 래브 등의 각 하위 주제를 가지고) 각 조상).

나는 주위를보고 AncestryActs As Tree을 우연히 발견하고 코드를 읽고 그들을 사용에 내 손을 시도했지만 액세스하고 데이터를 당겨 반대로 그들은 더 사물의 조상 측의 주위에 중심 보인다 봤는데 어린이.

는 또한 협회

has_many :through, and has_and_belongs_to_many 

를 사용에서 내 손을 시도했지만 작동 탐색 시스템을 구축하는 나의 시도에 실패왔다. 그리고이 일을하는 방법에 대해 내 머리를 감싸는 것을 완료하는 것처럼 보일 수 없습니다.

그런 곤경에 처한 사람에게는 어떤 생각이나 제안이 있습니까? 또는 이러한 기능을 제공하는 보석에 대해 알고 계십니까?

관계 클래스 & 모델 : (이것은 스트림처럼 흘러야로)

class CreateStreams < ActiveRecord::Migration 
    def change 
    create_table :streams do |t| 
     t.integer :downstream_id 
     t.integer :upstream_id 

     t.timestamps 
    end 

    add_index :streams, :downstream_id 
    add_index :streams, :upstream_id 
    add_index :streams, [:downstream_id, :upstream_id], unique: true 
    end 
end 





# == Schema Information 
# 
# Table name: streams 
# 
# id   :integer   not null, primary key 
# downstream_id :integer 
# upstream_id :integer 
# created_at :datetime  not null 
# updated_at :datetime  not null 
# 

class Stream < ActiveRecord::Base 
    attr_accessible :downstream_id 

    belongs_to :subsidiary, class_name: "Topic" 
    belongs_to :tributary, class_name: "Topic" 

    validates :downstream_id, presence: true 
    validates :upstream_id, presence: true 

end 

주제 모델

# == Schema Information 
# 
# Table name: topics 
# 
# id   :integer   not null, primary key 
# name  :string(255) 
# created_at :datetime  not null 
# updated_at :datetime  not null 
# slug  :string(255) 
# wiki  :string(255) 
# summary :string(255) 

class Topic < ActiveRecord::Base 
    extend FriendlyId 
    attr_accessible :name, :wiki, :summary 

    has_many :streams, foreign_key: "downstream_id", dependent: :destroy 
    has_many :tributaries, through: :streams, source: :tributary 
    has_many :reverse_streams, foreign_key: "upstream_id", 
          class_name: "Stream", 
          dependent: :destroy 
    has_many :subsidiaries, :through => :reverse_streams, source: :subsidiary 


    friendly_id :name, use: :slugged 

    validates :name, presence: true, length: { maximum: 50 }, 
        uniqueness: { case_sensitive: false } 

    def downstream?(other_topic) 
    streams.find_by_downstream_id(other_topic.id) 
    end 

    def flow!(other_topic) 
    streams.create!(downstream_id: other_topic.id) 
    end 

    def dam!(other_topic) 
    streams.find_by_downstream_id(other_topic.id).destroy 
    end 
end 

참고 : 나는 또한 하위 항목, 여러 부모를 할당 할 수 있어야합니다. 예를 들어 캐릭터가 잠재적으로 "액터"의 밑에 놓일 수 있습니다.

+0

모델과 테이블의 출처를 보여줄 수 있습니까? –

+0

추가 된 주제 모델 및 스트림 (관계) 모델/테이블 여러 개의 마이그레이션에 걸쳐 퍼져 있지만 주제 테이블을 추가 할 수 있으므로 주석 버전을 추가했습니다 –

답변

0

간단한 방법으로 이것을 설정하려면 재귀 관계로 가야합니다. 이것은 주제 (중첩) 다른 주제에 속할 수 있다는 것을 의미합니다

화제의 데이터베이스 모델과 같습니다 topic_id

  • ID
  • 주제

  • 제목

모델은 다음과 같습니다.

class Topic < ActiveRecord::Base 
    belongs_to :topic 
    has_many :topics 
end 

이제 주제가있는 경우 .topic과 그 하위를 .topics으로 지정하여 부모에게 액세스 할 수 있습니다. 부모가없는 주제는 최상위 주제이고 하위 노드가없는 주제는 끝 노드입니다.

+0

죄송합니다. 부모님이 왜 has_many : through와 h_a_b_t_m 데이터 모델을 따라 가는지 알고 있습니다. –