【发布时间】:2016-02-08 17:24:13
【问题描述】:
我在我的 rails 应用程序中重新打开了一个 gem 类,一切似乎都很好,一切运行良好,但几分钟后,我的应用程序似乎忘记了所有修改:
config/initializers/rapidfire_custom.rb
::Rapidfire::Survey.class_eval do
belongs_to :campaign, class_name: '::Campaign', inverse_of: :survey
before_create :default_active
before_validation :default_status, :default_name
STATUS = ['DRAFT', 'PUBLISHED']
validates_inclusion_of :status, in: STATUS
validates :name, presence: true
scope :from_company, -> (id) { where(company_id: id) }
scope :template, -> { where(campaign_id: nil) }
scope :published, -> { where(status: 'PUBLISHED') }
def default_active
self.active ||= true
end
def default_status
self.status ||= 'DRAFT'
end
def self.all_status
STATUS
end
def default_name
if self.campaign
self.name = 'Survey'
end
end
end
错误:
undefined method `template' for #<Rapidfire::Survey::ActiveRecord_AssociationRelation:0x007fc762215ad0>
它只发生在开发中,我使用的是 puma:
config/puma.rb
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
if ENV['RACK_ENV'].nil? || ENV['RACK_ENV'] == 'development'
ssl_bind 'my_website.dev', '3000', { key: 'ssl/device.key', cert: 'ssl/device.crt' }
end
on_worker_boot do
ActiveRecord::Base.establish_connection
$redis.client.reconnect
end
我不知道发生了什么,有什么想法吗?
谢谢
【问题讨论】:
-
有什么原因你在猴子修补类而不只是子类化它?
-
我按照您的建议使用了继承,并且效果很好。但我仍然不明白为什么这不适用于我之前的猴子补丁
标签: ruby-on-rails ruby metaprogramming