code-quality

Pretend with rails generator

how to pretend rails generate to see if generated files will conflict

Pretend with rails generator

The rails generate command will be used many times during the lifetime of a project. Sometimes the generate may create or modify multiple files that you may not want to. If you need to find out what files will be created/modified, you can run a pretend generation. Here let’s try generating model that will conflict with already created model:

$ rails g model ticket owner:string status:integer assigned:string subject:string next_step:enum type:enum -p
      invoke  active_record
    conflict    db/migrate/20160510153738_create_tickets.rb

Now, let’s pretend to generate model that has never been created in the app before:

$ rails g model comment body:text post_id:integer -p
      invoke  active_record
      create    db/migrate/20160510153812_create_comments.rb
      create    app/models/comment.rb

In the first case we were told that migration file has already been created and will conflict while second generator ran smoothly. Since we were just pretending, no new files were actually created but we were able to analyse when generate command would create conflicting files in project.

I am yet to really make use of this feature but hopefully it will come handy someday in future.

References:

Press Esc to return to the article
About the author

Prakash Poudel Sharma

Independent product manager & agentic engineering consultant

I'm an independent product manager, founder, and software builder based in Kathmandu. I help teams make product decisions and adopt agentic software development, drawing on over a decade of building software. I write about product thinking, engineering with AI, and hands-on experiments.

Keep reading

More on this

Join the conversation0 comments

What did you take away?

Thoughts, pushback, or a story of your own? Drop a reply below — I read every one.

Comments are powered by Disqus. By posting you agree to theirterms.

0:000:00