2013-03-20 34 views
1

這是一個關於如何在Ruby中定義方法和類以及如何讀取API文檔的問題。簡而言之:如何定義的create_table方法/進口/繼承,因此它在ActiveRecord::Migration可用,在這樣的代碼:這些方法如何在Rails Migration類中使用create_table?

class CreateProducts < ActiveRecord::Migration 
    def up 
    create_table :products do |t| 
     t.string :name 
     ... 

從Java世界的到來,我預計API docs提供指向基類或混入其中,一切都被定義了。但是,如果我查看Migration的頁面,我會看到一些有用的手寫文檔,但沒有鏈接到create_table的最詳細的權威定義。我用谷歌找到它ActiveRecord::ConnectionAdapters::SchemaStatements,但即使看着這兩個文件的源代碼:

Migration in git
create_table in git

我看不出create_table被帶入範圍在Migration。該類中動態創建

回答

1

這些方法:https://github.com/rails/rails/blob/d68e299167c8da07dc63a55197313b5c3396c3a4/activerecord/lib/active_record/migration/command_recorder.rb

它們在CommandRecorder類動態添加的,那麼這行:

class Migration 
    autoload :CommandRecorder, 'active_record/migration/command_recorder' 

使其可供遷移類。

+0

我認爲這些方法僅在類eval的類CommandRecorder中定義。 – Kaeros 2013-03-20 22:31:55

+0

@Kaeros感謝您的糾正,從我的手機中查看它。我相應編輯。 – fmendez 2013-03-20 22:40:01

+0

在遷移下沒有加載CommandRecorder?所以我可以說'Migration :: CommandRecorder.new.create_table',但不是'Migration.new.create_table'。 – 2013-03-21 15:37:15

相關問題