2016-04-12 101 views
0

私はRails 4アプリケーションで gemを使用しています。祖先の私のモデルは次のようになります:has_ancestryメソッドが見つからないため、祖先のモデルに関連するマイグレーションが発生しました。

class Product < ActiveRecord::Base 

    has_ancestry 
    has_many :company_products, dependent: :destroy 
    has_many :companies, through: :company_products 

    validates_presence_of :name 

end 

このコードは機能します。私はActiveAdminインターフェースでそれを管理することができ、そのようなモデルに対するHABTM関係のネストされたチェックボックスセットを作ることさえできます。

このコードの移行を実行するために、(ファイル名を:20160412201550_add_code_to_products.rb):しようとしたときただし、

class AddCodeToProducts < ActiveRecord::Migration 

    def change 
    change_table :products do |table| 
     table.string :code, limit: 100, null: false, default: '' 
    end 
    reversible do |direction| 
     direction.up do 
     {passage: 'Passage', harbor: 'Harbor'}.each do |k, v| 
      index = 0 
      Product.where(name: v).each do |product| 
      code = index > 0 ? "#{k}#{index}" : k 
      product.update! code: code 
      end 
     end 
     end 
     direction.down do 
     # nothing here 
     end 
     add_index :products, :code, unique: true 
    end 
    end 

end 

叫んで、Productを含む文章に到達したときにそれは爆発:

NameError: undefined local variable or method `has_ancestry' for #

スタックトレースには追加行が表示されます。関連するものである:Productモデルクラス定義でhas_ancestryラインである

/home/myusername/Proyectos/myproject/app/models/product.rb:3:in `<class:Product>' 

ので、簡単に:私は、標準のサーバ(rails s)を実行する場合

  • これは完璧に動作します。
  • これは、移行を実行すると爆発的になります。 いいえrequire 'ancestry'を呼び出すと、別の例外が発生するため、修正されません。LoadError: cannot load such file -- ancestry

マイグレーションコンテキストで祖先をロードするにはどうすればよいですか?

+0

上で利用できるようにするに。あなたのマイグレーションで '祖先を必要とする '試みましたか? – trh

+0

はい(正しく引用されていますが、はい) –

+0

heh ..申し訳ありませんが、私はあなたの最後の声明を誤解し、なぜあなたが必要としていなかったのか理解しようとしていました。 – trh

答えて

1

ancestryはバグです。 gemfileでそれを修正:

gem 'ancestry', require: true 

興味深い移行コンテキスト

関連する問題