Last active
August 18, 2021 13:06
-
-
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
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
# 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 |
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
... 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