为什么我的关注方法不会从控制台运行?

时间:2017-10-15 02:41:57

标签: ruby-on-rails activesupport-concern

我创建了一个问题,即添加enumclass_methods

# app/models/concerns/addressable.rb
module Addressable
  extend ActiveSupport::Concern

  included do
    enum address_province: {
      Queen: 'Q',
      Bear:  'B'
    }
  end

  class_methods do
    def options_for_cities
      %w[a b]
    end

    def scream
      'aaaaaaaaaaaaaaaaaa'
    end
  end
end

# app/models/order.rb
class Order < ApplicationRecord
  include Addressable
end

然而,当我尝试在Order中执行任何这些方法时,它们都无法正常工作。

Running via Spring preloader in process 3004
Loading development environment (Rails 5.1.2)

irb(main):001:0> Order.options_for_cities
NoMethodError: undefined method `options_for_cities' for Order (call 'Order.connection' to establish a connection):Class
Did you mean?  options_for_with_status
    from (irb):1
irb(main):002:0> Order.scream
NoMethodError: undefined method `scream' for Order (call 'Order.connection' to establish a connection):Class
    from (irb):2
irb(main):003:0> Order.address_provinces
NoMethodError: undefined method `address_provinces' for Order (call 'Order.connection' to establish a connection):Class
    from (irb):3

2 个答案:

答案 0 :(得分:1)

您似乎与现有的Addressable模块存在冲突。尝试将模块名称更改为其他名称。

答案 1 :(得分:0)

这些不是问题。所有发生的事情都是在需要之前没有实例化连接。

尝试

Order.connection

Order.count

你得到了订单的欲望。