【问题标题】:Undefined class method in Rails model using ActiveModel使用 ActiveModel 的 Rails 模型中未定义的类方法
【发布时间】:2015-06-26 13:57:10
【问题描述】:

我有一个无数据库(没有active_record)Rails 4.2 应用程序,它有两个使用ActiveModel::Model 模块的无表模型。

(发布的代码是简化的,但结果是真实的,无论如何都是一样的。)


第一个模型:Group,定义在app/models/group.rb

class Group
    include ActiveModel::Model

    def self.works?
        true
    end
end

在应用程序和控制台中一切正常:

irb(main):001:0> Group.works?
=> true
irb(main):002:0> Group.instance_methods
=> [:model_name, :validation_context, :validation_context=, :_validate_callbacks, :_validate_callbacks?, :_validate_callbacks=, :_run_validate_callbacks, :_validators, :_validators?, :_validators=, :persisted?, :to_model, :to_key, :to_param, :to_partial_path, :validates_absence_of, :validates_acceptance_of, :validates_confirmation_of, :validates_exclusion_of, :validates_format_of, :validates_inclusion_of, :validates_length_of, :validates_size_of, :validates_numericality_of, :validates_presence_of, :run_callbacks, :errors, :valid?, :validate, :invalid?, :read_attribute_for_validation, :run_validations!, :validates_with, :blank?, :present?, :presence, :acts_like?, :duplicable?, :deep_dup, :try, :try!, :in?, :presence_in, :to_query, :instance_values, :instance_variable_names, :to_json_with_active_support_encoder, :to_json_without_active_support_encoder, :to_json, :as_json, :with_options, :html_safe?, :is_haml?, :psych_to_yaml, :to_yaml, :to_yaml_properties, :`, :require_or_load, :require_dependency, :load_dependency, :unloadable, :pretty_print, :pretty_print_cycle, :pretty_print_instance_variables, :pretty_print_inspect, :nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :gem, :silence_warnings, :enable_warnings, :with_warnings, :silence_stderr, :silence_stream, :suppress, :capture, :silence, :quietly, :class_eval, :pretty_inspect, :byebug, :debugger, :concern, :suppress_warnings, :==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]

第二个模型:Banner,在app/models/banner.rb 中定义,代码与组中完全相同:

class Banner
    include ActiveModel::Model

    def self.works?
        true
    end
end

但是,这个不起作用:

irb(main):001:0> Banner.works?
NoMethodError: undefined method `works?' for Banner:Module (trace omitted)
irb(main):002:0* Banner.instance_methods
=> []

我已经尝试对模型做一些讨厌的事情,冗余添加require "active_model"(一些帖子表明由于类的延迟加载或其他原因可能会出现这样的问题,这可以解决它),以及直接复制粘贴从一个模型到另一个模型的代码,只改变类名(就像在示例中一样)但没有运气。

【问题讨论】:

  • 在 Rails 项目中的某处检查横幅模块。它与您认为的横幅类不同。
  • Sergio 在下面回答了您的问题,但附带说明:您使用的是instance_methods,而您实际上是在寻找methods(如:类方法)。所以Banner.methods.include?(:works?) 应该是true

标签: ruby-on-rails ruby ruby-on-rails-4 activemodel


【解决方案1】:

仔细阅读错误

NoMethodError: undefined method `works?' for Banner:Module

看到Banner:Module?您还有其他一些 Banner,它是一个模块,它被加载到您的模型的位置(因此,隐藏它)。

这通常是由 Rails 的延迟加载引起的。至于这里有什么补救办法,就看你的具体情况了。常见的选项有:

  • 不进行延迟加载(而是急切地加载类)
  • 指定 FQN(完全限定名称),以防这是本地名称与全局名称冲突。
  • 明确要求模型文件(类似于 `require 'app/models/banner.rb')

【讨论】:

  • 感谢您让我意识到应用程序本身被称为横幅。这意味着在application.rb 我有一个module Banner,这就是这一切的原因。更改它可以解决所有问题。
猜你喜欢
  • 1970-01-01
  • 2016-11-04
  • 2015-12-10
  • 2012-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
  • 2023-04-01
相关资源
最近更新 更多