Last active
January 28, 2019 01:48
-
-
Save mmahalwy/a922a82cba31c942dadbde68ebfe87a9 to your computer and use it in GitHub Desktop.
Generating Factories after model create for Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.eager_load! | |
ApplicationRecord.subclasses.reject { |m| m.name =~ /HABTM/ || m.name =~ /Pg/ }.each do |model| | |
begin | |
model_relation = model.reflect_on_all_associations.map{ |r| [ r.foreign_key, r.name ] }.to_h | |
columns = model.columns.map { |c| [ c.name, c.type ] }.to_h | |
model_relation.keys.each { |k| columns.delete(k) } | |
puts "rails g factory_bot:model #{model.name} #{columns.map { |k, v| "#{k}:#{v}" }.join(' ')}" | |
`rails g factory_bot:model #{model.name} #{columns.map { |k, v| "#{k}:#{v}" }.join(' ')}` | |
rescue => e | |
puts e.inspect | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment