While Pravin pointed in the right direction, I found that it was not easy to realize. I did the following: I added a file to config/initializers (the name does not matter) containing the following:
require 'active_support' require 'active_record' class YourApplication module SchemaDefinitions module ExtraMethod def attachment(*args) options = args.extract_options! args.each do |col| column("#{col}_identifier", :string, options) column("#{col}_extension", :string, options) column("#{col}_size", :integer, options) end end end def self.load! ::ActiveRecord::ConnectionAdapters::TableDefinition.class_eval { include YourApplication::SchemaDefinitions::ExtraMethod } end end end ActiveSupport.on_load :active_record do YourApplication::SchemaDefinitions.load! end
then you can just do something like:
rails g model Person name:string title:string avatar:attachment
which will create the following migration:
def self.up create_table :people do |t| t.string :name t.string :title t.attachment :avatar t.timestamps end end
If you start the migration, rake db:migrate , it will create the following Person model:
ruby-1.9.2-p0 > Person => Person(id: integer, name: string, title: string, avatar_identifier: string, avatar_extension: string, avatar_size: integer, created_at: datetime, updated_at: datetime)
Hope this helps!
nathanvda
source share