我在Rails 4应用程序中使用了祖先 gem。我的祖先模型如下所示:
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:用于#的未定义局部变量或方法`has_ancestry‘
堆栈跟踪显示附加行。有关的问题是:
/home/myusername/Proyectos/myproject/app/models/product.rb:3:in `<class:Product>'它是has_ancestry模型类定义中的Product行。
因此,简单地说:
rails s),这是非常有效的。require 'ancestry'不会修复它,因为它会引发另一个异常:LoadError: cannot load such file -- ancestry。如何为迁移上下文加载祖先?
发布于 2016-04-12 22:08:02
看来ancestry是个小混混。修正了宝石文件中的内容:
gem 'ancestry', require: true使其可用于迁移上下文
https://stackoverflow.com/questions/36584136
复制相似问题