I have an array of strings:
days = ["Monday", "Tuesday", "Wednesday"]
I'd like to create a Day in the database named after each of these:
days.each do |day|
Day.create(name: day)
end
This isn't so cute, however. In the past, I have been able to clean up lists of attributes by passing them into attr_accessor with a splat:
attr_accessor *ATTRIBUTES
I am looking to be able to do something like this:
Day.create(name: *days)
Is a similar interface available for ActiveRecord?
You can create them in one go:
Day.create(days.map { |day| {name: day} })
create
docs mentioned:
Creates an object (or multiple objects) and saves it to the database, if validations pass. The attributes parameter can be either a
Hash
or anArray
ofHashes
.