Rails 3 ActiveModel:不能直接包含ActiveModel :: Model

mis*_*ker 7 ruby-on-rails activemodel rails-activerecord

在我尝试拥有一个活动模型的Rails 3.2.11和"开发"环境中:

class DisponibilityApi
  include ActiveModel::Model

  attr_accessor :start_time, :end_time 
  validates :start_time, :end_time, :presence => true

end
Run Code Online (Sandbox Code Playgroud)

我有一个错误:

NameError:未初始化的常量ActiveModel :: Model

但是当我手动包含它时:

class DisponibilityApi
  extend  ActiveModel::Naming
  extend  ActiveModel::Translation
  include ActiveModel::Validations
  include ActiveModel::Conversion

  attr_accessor :start_time, :end_time 
  validates :start_time, :end_time, :presence => true

end
Run Code Online (Sandbox Code Playgroud)

现在它有效!

我错过了什么吗?

谢谢 !

t_i*_*chy 17

ActiveModel :: Model是Rails 4的新功能,这就是为什么它出现在Github master上,而不是出现在3.x gems中.如果你查看Github上的3.x版本分支,它也不存在.

https://github.com/rails/rails/tree/3-2-stable/activemodel/lib/active_model

对于Rails 3.x,您需要手动包含每个模块.

要查看它包含的内容,请查看master分支中的文件.

https://github.com/rails/rails/blob/master/activemodel/lib/active_model/model.rb


Int*_*idd -2

看来 ActiveModel::Model 模块不再存在,您必须手动包含模型所需的模块。

\n\n

即使文档声明该模块仍然存在,快速查看 ~/.rvm 文件夹会证明不再存在 model.rb 文件:

\n\n
activemodel-3.2.11/lib \xc2\xbb pwd                   \n/Users/Intrepidd/.rvm/gems/ruby-1.9.3-p327-turbo/gems/activemodel-3.2.11/lib\nactivemodel-3.2.11/lib \xc2\xbb ls\nactive_model    active_model.rb\nactivemodel-3.2.11/lib \xc2\xbb ls -l active_model\ntotal 280\n-rw-r--r--   1 Intrepidd  staff  16574  9 Jan 00:39 attribute_methods.rb\n-rw-r--r--   1 Intrepidd  staff   4556  9 Jan 00:39 callbacks.rb\n-rw-r--r--   1 Intrepidd  staff   2338  9 Jan 00:39 conversion.rb\n-rw-r--r--   1 Intrepidd  staff   4879  9 Jan 00:39 dirty.rb\n-rw-r--r--   1 Intrepidd  staff  12087  9 Jan 00:39 errors.rb\n-rw-r--r--   1 Intrepidd  staff   5259  9 Jan 00:39 lint.rb\ndrwxr-xr-x   3 Intrepidd  staff    102  9 Jan 00:39 locale\ndrwxr-xr-x   4 Intrepidd  staff    136  9 Jan 00:39 mass_assignment_security\n-rw-r--r--   1 Intrepidd  staff   8720  9 Jan 00:39 mass_assignment_security.rb\n-rw-r--r--   1 Intrepidd  staff   6478  9 Jan 00:39 naming.rb\n-rw-r--r--   1 Intrepidd  staff   4257  9 Jan 00:39 observer_array.rb\n-rw-r--r--   1 Intrepidd  staff   8163  9 Jan 00:39 observing.rb\n-rw-r--r--   1 Intrepidd  staff     38  9 Jan 00:39 railtie.rb\n-rw-r--r--   1 Intrepidd  staff   2939  9 Jan 00:39 secure_password.rb\n-rw-r--r--   1 Intrepidd  staff   4304  9 Jan 00:39 serialization.rb\ndrwxr-xr-x   4 Intrepidd  staff    136  9 Jan 00:39 serializers\n-rw-r--r--   1 Intrepidd  staff    319  9 Jan 00:39 test_case.rb\n-rw-r--r--   1 Intrepidd  staff   2339  9 Jan 00:39 translation.rb\ndrwxr-xr-x  13 Intrepidd  staff    442  9 Jan 00:39 validations\n-rw-r--r--   1 Intrepidd  staff   7961  9 Jan 00:39 validations.rb\n-rw-r--r--   1 Intrepidd  staff   6227  9 Jan 00:39 validator.rb\n-rw-r--r--   1 Intrepidd  staff    172  9 Jan 00:39 version.rb\n
Run Code Online (Sandbox Code Playgroud)\n\n

这很有趣,因为这个文件仍然存在于 github 上,但不在 .gem 中。

\n