【问题标题】:Ruby on Rails inheritance syntax [duplicate]Ruby on Rails 继承语法 [重复]
【发布时间】:2014-12-20 06:25:04
【问题描述】:

鉴于这段代码:

 class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.string :commenter
      t.text :body
      t.references :article, index: true

      t.timestamps
    end
  end
end

我了解 CreateComments 继承自 Migration 的方法。但是什么是迁移到 ActiveRecord?与其他语言相比,我在 ActiveRecord 类中找不到任何名为 Migration 的成员。那么:: 是什么意思呢?在这种情况下,您将什么称为迁移?

【问题讨论】:

    标签: ruby-on-rails ruby activerecord


    【解决方案1】:

    这里实际上Migration 是一个类,ActiveRecord 是模块(命名空间)。所以定义是:

    module ActiveRecord
        class Migration
            ....
        end
    end
    

    Migration的基本定义在文件/activerecord/lib/active_record/migration.rb

    所以,为了调用类迁移,你应该指定它在哪个模块中,因此你有ActiveRecord::Migration

    【讨论】:

    • 模块类似于其他语言的命名空间。
    【解决方案2】:

    ActiveRecord 是一个命名空间,Migration 是一个类
    文档中的更多信息
    http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-08
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多