我想在我的迁移文件中插入COMMENT,它是sql命令的一部分.
As far as I know,I can add COMMENT to each table and column.
t.string :name,:comment => "A user's fullname" t.string :label,:comment => "name of color" t.text :value,:comment => "self intro" t.integer :position,:comment => "1 is left,2 is right"
神奇的语句被翻译成sql,就像
create table test ( name varchar(255) not null COMMENT 'blahblah',label varchar(255) null COMMENT 'hahaha' text varchar(255) not null,position int(11) );
有人知道插件的名字吗?
>我不是在寻找Dave Thomas的Annotate Models plugins.我的意思是评论是在MySQL查询中.
解决方法
我不知道任何能完成你要求的插件.您可以通过查看ActiveRecord :: ConnectionAdapters :: ColumnDefinition来破解您想要的内容. (请参阅active_record / connection_adapters / abstract / schema_definitions.rb.)
正如您所看到的,Struct定义了各种列选项(例如:limit和:default.)您可以使用:comment扩展该结构,然后修改#to_sql以生成所需的sql.您还需要修改TableDefinition#列以设置:comment属性.
module ActiveRecord module ConnectionAdapters class ColumnDefinition attr_accessor :comment def to_sql_with_comment column_sql = to_sql_without_comment return column_sql if comment.nil? "#{column_sql} COMMENT '#{base.quote_string(comment)}'" end alias_method_chain :to_sql,:comment end class TableDefinition # Completely replaces (and duplicates the existing code,but there's # no place to really hook into the middle of this.) def column(name,type,options = {}) column = self[name] || ColumnDefinition.new(@base,name,type) if options[:limit] column.limit = options[:limit] elsif native[type.to_sym].is_a?(Hash) column.limit = native[type.to_sym][:limit] end column.precision = options[:precision] column.scale = options[:scale] column.default = options[:default] column.null = options[:null] column.comment = options[:comment] @columns << column unless @columns.include? column self end end end end