2

トピックにはサブトピックがあり、サブトピックは元のトピックから約6レベル下がっています。これらのトピックのそれぞれには、複数のサブトピックがあります。 私はこのデータをトラバースし、それぞれのサブトピックから提出されたデータを戻す方法を探しています。Railsアプリケーションにおける複合データ構造のトラバース

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

(、slitherin家はメンバーのそれぞれのサブトピックを持っていることなどマルフォイ、クラッブを、と仮定)私は(メンバーのそれぞれのためのビデオはSlitherin_House、文字、およびHarry_Potterのためのビデオ・リストに表示したいです各祖先)。

は、私の周り見て、 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データモデルの後ろにいたのです。 –

関連する問題