ruby - Rails customize class Gem -
i reopened gem class in rails app, seems ok, runs nicely after few minutes, seems app forgot modifications:
config/initializers/rapidfire_custom.rb
::rapidfire::survey.class_eval 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
the error:
undefined method `template' #<rapidfire::survey::activerecord_associationrelation:0x007fc762215ad0>
it happens on development, i'm using 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 activerecord::base.establish_connection $redis.client.reconnect end
i don't know what's going on, idea ?
thanks
i'm guessing when activerecord instantiates rapidfire::survey
records loads class gem - nullifies changes have done class eval in initializer.
monkey-patching objects not belong should done if need override library method , there no better way.
you need consider when , class eval happens - initializers run when rails app loading while model classes lazy loaded when activerecord needs them. using initializer configure model not idea.
using inheritance correct thing here - you're creating own domain object.
Comments
Post a Comment