2016-12-29 16 views
0

私の系譜データベース用のツリーを構築するには、宝石closure_treeを試してみたいです。 私はRuby MRI 2.3.0の下でWindows 7のボックスに宝石をインストールしました。 依存関係のバージョンは以下のエラーログにあります。私はそれが失敗した最も最小限のコードをしようとすると、私はRailsのを使用していないと、再び私はtag_hierachiesRuby closure_tree gemをレールなしで使用する

私の質問があるテーブルを作成すると推定移行を行いませんので

$ gem install closure_tree 
Fetching: with_advisory_lock-3.0.0.gem (100%) 
Successfully installed with_advisory_lock-3.0.0 
Fetching: closure_tree-6.2.0.gem (100%) 
Successfully installed closure_tree-6.2.0 
2 gems installed 

、私は考えます:CAN私はこの宝石をレールなしで使用しています。

require 'active_record' 
require 'closure_tree' 

ActiveRecord::Base.establish_connection(
    :adapter => "sqlite3", 
    :database => "sample.db" 
) 

if !ActiveRecord::Base.connection.table_exists?('tags') 
    ActiveRecord::Schema.define do 
    create_table :tags do |table| 
     table.column :name, :string 
     table.column :parent_id, :integer 
    end 
    end 
end 

class Tag < ActiveRecord::Base 
    has_closure_tree 
end 

grandparent = Tag.create(name: 'Grandparent') 

d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/sqlite3_adapter.rb:513:in `table_structure': Could not find table 'tag_hierarchies' (ActiveRecord::StatementInvalid) 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/sqlite3_adapter.rb:387:in `columns' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/schema_cache.rb:43:in `columns' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attributes.rb:93:in `columns' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attributes.rb:98:in `columns_hash' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/inheritance.rb:205:in `subclass_from_attributes?' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/inheritance.rb:54:in `new' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/persistence.rb:50:in `create!' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/closure_tree-6.2.0/lib/closure_tree/hierarchy_maintenance.rb:65:in `block in rebuild!' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/closure_tree-6.2.0/lib/closure_tree/support.rb:108:in `block (2 levels) in with_advisory_lock' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:220:in `transaction' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/closure_tree-6.2.0/lib/closure_tree/support.rb:108:in `block in with_advisory_lock' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/with_advisory_lock-3.0.0/lib/with_advisory_lock/base.rb:77:in `yield_with_lock' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/with_advisory_lock-3.0.0/lib/with_advisory_lock/base.rb:65:in `yield_with_lock_and_timeout' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/with_advisory_lock-3.0.0/lib/with_advisory_lock/base.rb:48:in `with_advisory_lock_if_needed' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/with_advisory_lock-3.0.0/lib/with_advisory_lock/concern.rb:16:in `with_advisory_lock_result' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/with_advisory_lock-3.0.0/lib/with_advisory_lock/concern.rb:10:in `with_advisory_lock' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/closure_tree-6.2.0/lib/closure_tree/support.rb:107:in `with_advisory_lock' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/closure_tree-6.2.0/lib/closure_tree/hierarchy_maintenance.rb:63:in `rebuild!' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/closure_tree-6.2.0/lib/closure_tree/hierarchy_maintenance.rb:39:in `_ct_after_save' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:432:in `block in make_lambda' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:228:in `block in halting_and_conditional' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:506:in `block in call' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:506:in `each' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:506:in `call' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:92:in `__run_callbacks__' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in `_run_save_callbacks' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/callbacks.rb:302:in `create_or_update' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/persistence.rb:120:in `save' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/validations.rb:37:in `save' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attribute_methods/dirty.rb:21:in `save' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:286:in `block (2 levels) in save' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:220:in `transaction' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:348:in `with_transaction_returning_status' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:286:in `block in save' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:301:in `rollback_active_record_state!' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:285:in `save' 
    from d:/Ruby/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/persistence.rb:34:in `create' 
    from C:/Users/Gebruiker/BoxSync/ruby_werk/acts_as_tree/closure_tree.rb:22:in `<main>' 
+0

は、この記事を見つけた非常に便利https://blog.openshift.com/tmp-part-2-using-clojure-tree- with-activerecord/ – peter

答えて

3

を与えるREADMEのインストール手順は、心の中で唯一のRailsに焦点を当てているようです。ただし、tag_hierarchiesという名前の別のテーブル(現在のtagsテーブルに加えて)が必要であることがわかります。

Railsでは、このテーブルの生成はインストール手順の一部と思われます。リンクされたドキュメントの手順5。

したがって、次の内容のtag_hierarchiesテーブルを作成する必要があります。これは、テーブル構造に関する限りrails g closure_tree:migration tagが生成するものです。 create_hierarchies_table.rb.erbを参照してください。このリンクされたファイルのようにインデックスを追加することもできます。

だから、私のために予想されるとして働いていた次のようにスクリプトを更新:

require 'active_record'                         
require 'closure_tree'                         

ActiveRecord::Base.establish_connection(                    
    :adapter => "sqlite3",                        
    :database => "sample.db"                       
)                              

if !ActiveRecord::Base.connection.data_source_exists?('tags')               
    ActiveRecord::Schema.define do                      
    create_table :tags do |table|                      
     table.column :name, :string                      
     table.column :parent_id, :integer                     
    end                             
    end                             
end                              

# You also need a corresponding model's table name followed by "_hierarchies" table.         
#                              
# Migration created based on                       
# `https://github.com/mceachen/closure_tree/blob/master/lib/generators/closure_tree/templates/create_hierarchies_table.rb.erb` 
if !ActiveRecord::Base.connection.data_source_exists?(:tag_hierarchies)             
    ActiveRecord::Schema.define do                      
    create_table :tag_hierarchies do |table|                   
     table.integer :ancestor_id, null: false                   
     table.integer :descendant_id, null: false                   
     table.integer :generations, null: false                   
    end                             
    end                             
end                              

class Tag < ActiveRecord::Base                       
    has_closure_tree                          
end                              

grandparent = Tag.create(name: 'Grandparent')                   

grandparent.children.create(name: 'Father')                    
grandparent.children.create(name: 'Uncle')                    
+0

素晴らしい作品、thnx vee – peter

+1

Heya、ここClosureTreeの著者。テストをチェックアウトして、多くのサンプルモデルの設定を確認してください。より多くの会話型ソリューションが必要な場合は、readmeにリンクされたgitterチャットルームがあります。 – mrm

+1

@mrm、テストに関するポイントありがとう。 'README.md'で示唆しておかなければならない点は、サンプルレール以外のサンプルを含めることです。また、インストールのステップ5のメモは、「デフォルトではテーブル名はモデルのテーブル名に続き、_hierarchies」と表示されていましたが、テーブル名が単数である必要があります(例: "tag_hierarchies" 'Tag'モデルの' tags'テーブルのための "tags_hierarchies"の代わりに。 – vee

関連する問題