Skip to content

Instantly share code, notes, and snippets.

@fjfish
Last active August 18, 2021 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjfish/ef86a4361ea9680985424760bc57684d to your computer and use it in GitHub Desktop.
Save fjfish/ef86a4361ea9680985424760bc57684d to your computer and use it in GitHub Desktop.
Pass a model in and it prompts you for the values of the columns
# Construct a hash to create a given model for testing
# Pressing return ignores the column
def make_me_a model
{}.tap do |model_hash|
model.columns.dup.delete_if { |x| x.name.in?(%w[created_at updated_at]) }.each do |column|
name = column.name
puts "#{name}?"
value = gets.chomp
model_hash[name.to_sym] = value if value.length > 0
end
end
end
# Convenience method
def model_for model
model.create make_me_a model
end
@fjfish
Copy link
Author

fjfish commented Aug 18, 2021

... Pressing return doesn't add it to the hash

Usage

Account.create make_me_a(Account)

Or just

acc_hash = make_me_a(Account)

For console fun and games

@fjfish
Copy link
Author

fjfish commented Aug 18, 2021

In the console

load 'make_me_a.rb'

Then use as above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment