Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6686201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:08:41+00:00 2026-05-26T05:08:41+00:00

How do I add things like default values to migrations from an application template.

  • 0

How do I add things like default values to migrations from an application template.

For example in template.rb:

generate(:scaffold, "Thing title:string")

What would be the best way to add something like , :default => 'Foo Bar', :null => false to the line in the migration that creates the title column.

I have two ideas, but don’t know if either is possible.

First, can additional attributes be added to the column from the generator? Can default values be set when the rails g scaffold command is called?

Second, could I add a gsub_file line after the generate line in my template. I don’t know how to do gsub_file on a file where part of the file’s name is not known (the timestamp on the migration).

Rails-3.1.0
Ruby-1.9.2-p290
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:08:42+00:00Added an answer on May 26, 2026 at 5:08 am

    If you look at the help:

    $ rails generate model --help
    Usage:
      rails generate model NAME [field:type field:type] [options]
    [...]
    

    you won’t see any options for specifying anything more than field names and types. AFAIK, you have to edit the generated migration if you want other options.

    If you look at the source, you will see things like this:

    def generate(what, *args)
      log :generate, what
      argument = args.map {|arg| arg.to_s }.flatten.join(" ")
    
      in_root { run_ruby_script("script/rails generate #{what} #{argument}", :verbose => false) }
    end
    

    So maybe you have access to in_root (which apparently comes from Thor these days) to put you at the application root so that you can use Dir['db/migrate/*'] to find the migration file; you presumably know the filename except for the leading timestamp and the part of the filename that you know is unique because it maps to a class name. So, you should be able to find the migration file and then you can patch it up as needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Things like $log$ and $version$ which add data upon check-in to the file. I'm
I recently started using Masterpages, the thing is I would like to add text
I have a model like: class Thing(models.Model): title = models.CharField(_('Title'), max_length=255) I want to
What things do I need to consider when doing this: What to remove/manipulate/add Regards
in order to make things easier for users i want to add multiple keyword
I'm working on a module that, among other things, will add some generic 'finder'
class Ball: a = [] def __init__(self): pass def add(self,thing): self.a.append(thing) def size(self): print
I am trying to write an Add-In to PowerPoint that does basically one thing:
I would like to update the default Window style dynamically at runtime so I
I have a singleton class based on Joose and I would like to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.