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:

About the author

Prakash Poudel Sharma

Engineering Manager · Product Owner · Varicon

Engineering Manager at Varicon, leading the Onboarding squad as Product Owner. Eleven years of building software — first as a programmer, then as a founder, now sharpening the product craft from the inside of a focused team.

Keep reading

More on this

Join the conversation 0 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 their terms.