2

I have a migration that will dynamically create tables on fly per date. Something like this:

class CreateCollectorPeriodTable < ActiveRecord::Migration

  def self.create_with(name)  
    create_table name.to_sym do |t|
      t.string :text, :limit => 1024
    end
  end 
end

I want to create a model that will access this migration..

I did read this: Rails Generate Model from Existing Table?, but in another question someone explained why I shouldn't try and make one model fit many tables..

Any suggestions?

1
  • Could you explain a bit more the reasoning behind this? What are you trying to achieve? Commented Jan 12, 2011 at 21:24

1 Answer 1

3
class CreateCollectorPeriodTable < ActiveRecord::Migration
  # name should be plural
  # i.e.: name = 'chickens'
  def self.create_with(name)  
    create_table name.to_sym do |t|
      t.string :text, :limit => 1024
    end
    model_file = File.join("app", "models", name.singularize+".rb")
    model_name = name.singularize.capitalize
    File.open(model_file, "w+") do |f|
      f << "class #{model_name} < ActiveRecord::Base\nend"
    end
  end 
end
Sign up to request clarification or add additional context in comments.

2 Comments

This will work. Only it seems to me almost identical to rails g model <model-name> text:string, so not sure what the use-case is.
@nathanvda, absolutely :) for me it is quite strange task

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.