2010-11-23 1 views
0

Я создаю Rails 3.0.3 камень и не могу заставить его работать:Простой Railtie Расширение Active Record

# attached.rb 
module Attached 
    require 'attached/railtie' if defined?(Rails) 
    def self.include(base) 
    base.send :extend, ClassMethods 
    end 
    module ClassMethods 
    def acts_as_fail 
    end 
    end 
end 

# attached/railtie.rb 
require 'attached' 
require 'rails' 

module Attached 
    class Railtie < Rails::Railtie 
    initializer 'attached.initialize' do 
     ActiveSupport.on_load(:active_record) do 
     ActiveRecord::Base.send :include, Attached 
     end 
    end 
    end 
end 

Я получаю undefined local variable or method 'acts_as_fail', когда я добавить acts_as_fail к любому из моих ActiveRecord моделей. Пожалуйста помоги! Я очень расстроен этим, казалось бы, тривиальным кодом! Благодаря!

ответ

4

Вы определяете self.include (4-я строка вниз), когда правильный метод равен self.included.

+0

Частное лицо ладонь ... Большое спасибо Райан! – 2010-11-23 09:04:22

3

Вы можете упростить код, используя extend непосредственно:

# attached.rb 
module Attached 
    require 'attached/railtie' if defined?(Rails) 
    def acts_as_fail 
    end 
end 

# attached/railtie.rb 
require 'attached' 
require 'rails' 

module Attached 
    class Railtie < Rails::Railtie 
    initializer 'attached.initialize' do 
     ActiveSupport.on_load(:active_record) do 
     ActiveRecord::Base.send :extend, Attached 
     end 
    end 
    end 
end 

Это хорошо читать: http://yehudakatz.com/2009/11/12/better-ruby-idioms/

 Смежные вопросы

  • Нет связанных вопросов^_^