浏览 457 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2007-12-21 关键字: migrate create_table
搞了几天rails了,太magic了。所以搞不清楚,只知道调用。
今天深入去了解了下 create_table 大家给指点指点 先请看流程: 再看 SchemaStatement 类的create_table()代码 ruby 代码
大家明白了吧。 根据传递的参数,一步一步来创建 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 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2007-12-21
流程。。。。
|
|
| 返回顶楼 | |
|
时间: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 |
|
| 返回顶楼 | |



