我试图通过指向Module常量来设置属性的默认值,但事情不会计划。相关代码包括:
[lib/establishments.rb]
module Establishments
BAR = "Bar"
RESTAURANT = "Restaurant"
BENEFIT = "Benefit"
end
[20140321164012_add_type_to_discount]
include Establishments
class AddTypeToDiscount < ActiveRecord::Migration
def change
add_column :discounts, :type, :string, :default => Establishments::RESTAURANT
end
end
rake db:migrate
以上代码导致:
rake aborted!
An error has occurred, this and all later migrations canceled:
uninitialized constant Establishments/home/bob/Dev/app/db/migrate/20140321164012_add_type_to_discount.rb:1:in `<top (required)>'
在我看来,/lib
未找到rake
路径。我不确定如何影响它。
我也尝试过添加:
require 'lib/establishments.rb'
在迁移文件的顶部,但是抱怨没有这样的文件。
答案 0 :(得分:2)
尝试:
require "#{Rails.root}/lib/establishments"
然后您就可以使用Establishments::RESTAURANT
答案 1 :(得分:1)
使用require_relative '../../lib/establishments'
代替include Establishments
答案 2 :(得分:1)
感谢您的回答,一切都是有效的 - 正如我发现的那样(同样有效)
require File.expand_path('lib/establishments')