论坛首页 编程语言技术论坛

migrate 中 create_table 的初步探索

浏览 3136 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-12-21  
搞了几天rails了,太magic了。所以搞不清楚,只知道调用。
今天深入去了解了下 create_table 大家给指点指点

先请看流程:


再看 SchemaStatement 类的create_table()代码
ruby 代码
 
  1. # ====== Do not add a primary key column  
  2. #  create_table(:categories_suppliers, :id => false) do |t|  
  3. #    t.column :category_id, :integer  
  4. #    t.column :supplier_id, :integer  
  5. #  end  
  6. # generates:  
  7. #  CREATE TABLE categories_suppliers (  
  8. #    category_id int,  
  9. #    supplier_id int  
  10. #  )  
  11. #  
  12. # See also TableDefinition#column for details on how to create columns.  
  13. def create_table(table_name, options = {})  
  14.   table_definition = TableDefinition.new(self)  
  15.   table_definition.primary_key(options[:primary_key] || "id"unless options[:id] == false  
  16.   
  17.   yield table_definition  
  18.   
  19.   if options[:force]  
  20.     drop_table(table_name, options) rescue nil  
  21.   end  
  22.   
  23.   create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "  
  24.   create_sql << "#{quote_table_name(table_name)} ("  
  25.   create_sql << table_definition.to_sql  
  26.   create_sql << ") #{options[:options]}"  
  27.   execute create_sql  
  28. end  

大家明白了吧。
根据传递的参数,一步一步来创建 create_sql, 然后execute来执行。


字段的组合是用 yield table_definition 
具体可以去跟踪
TableDefinition 类(schema_definitions.rb 文件里面, path: gems\1.8\gems\activerecord-2.0.1\lib\active_record\connection_adapters\abstract )

通过这个跟踪,知道了很多东西,不错。

现在也了解选项:force是什么意思。

也了解了 some_method *args do |something|
                 end

这种书写方法的意思。 do ....  end就是 some_method 里面的
yield

   发表时间:2007-12-21  
流程。。。。
  • 描述: work_flow
  • 大小: 20.6 KB
0 请登录后投票
   发表时间:2007-12-21  
为什么以前的create_table 里面的 do |t| ... end 块

    create_table :users do |t|
      t.column :user_name, :string
      t.column :user_password, :string
    end


可以写成:
    create_table :users do |t|
      t.string :user_name
      t.string :user_password
    end


的原因是应为下面的这个代码块:

      def column(name, type, options = {})
        column = self[name] || ColumnDefinition.new(@base, name, type)
        column.limit = options[:limit] || native[type.to_sym][:limit] if options[:limit] or native[type.to_sym]
        column.precision = options[:precision]
        column.scale = options[:scale]
        column.default = options[:default]
        column.null = options[:null]
        @columns << column unless @columns.include? column
        self
      end

      %w( string text integer float decimal datetime timestamp time date binary boolean ).each do |column_type|
        class_eval <<-EOV
          def #{column_type}(*args)
            options = args.extract_options!
            column_names = args
            
            column_names.each { |name| column(name, '#{column_type}', options) }
          end
        EOV
      end



代码是在:
schema_definitions.rb
%RUBY_HOME%\gems\1.8\gems\activerecord-2.0.1\lib\active_record\connection_adapters\abstract
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics