How to create multiple records from array in Rails Console

Gibson

I want to create 7 categories in production, so I have an array for them:

categories = ["Industrial & Loft","Nórdico","Moderno","Clásico","Contemporaneo","Exótico","Rustico","Landing"]

I want to loop through the array in rails console, and create a new category for each of the items, but this wont work:

categories.each { |category| category.new}

It says: NoMethodError: undefined method `new' for "Industrial & Loft":String

What am I missing? Thanks

radshop

If Category is one of your model classes, then you need to capitalize it and then assign the value of the categories item to one of the model elements (such as name in my example):

categories.each { |c| Category.new(name: c)}

Edit: But remember that "new" doesn't save a record, so you might want to use create, which is new & save combined:

categories.each { |c| Category.create(name: c)}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create multiple records from a single textfield in Rails?

From Dev

rails how to parse array of permutations to create records

From Dev

Rails create one or create multiple records from a range

From Dev

How to create multiple records and handle validations with create method in rails?

From Dev

create multiple records from a comma separated entry in rails form

From Dev

Rails create multiple records at once from different models

From Dev

How to find index of array of records return from database in rails?

From Dev

rails first_or_create for multiple records

From Dev

Rails Create Multiple Records in Join Table

From Dev

Create multiple records with a simple form on Rails

From Dev

Create multiple records with a simple form on Rails

From Dev

Create multiple records in rails with collection_select

From Dev

How to create multiple arrays from one array?

From Dev

How to create multiple SparkContexts in a console

From Dev

Get all Rails records from an array of IDs

From Dev

How to create a where clause that returns no records in Rails?

From Dev

How to create a where clause that returns no records in Rails?

From Dev

create single field array from all the records

From Dev

Rails Activerecord: How to exclude records with multiple associated records

From Dev

Inserting multiple records into MySQL from an array

From Dev

How to get the count of all records of a model in Rails console

From Dev

How to get the count of all records of a model in Rails console

From Dev

Ruby on Rails: create records for multiple models with one form and one submit

From Dev

Viewing Nested Records in Rails Console

From Dev

How to create route to array in rails

From Dev

how to create multiple checkbox from associative array in angular js

From Dev

How to Store Multiple records in My Array List from Azure mobile service Using API Code?

From Dev

Rails - Unable to create records from csv file on VPS

From Dev

How to display associations from rails console

Related Related

  1. 1

    How to create multiple records from a single textfield in Rails?

  2. 2

    rails how to parse array of permutations to create records

  3. 3

    Rails create one or create multiple records from a range

  4. 4

    How to create multiple records and handle validations with create method in rails?

  5. 5

    create multiple records from a comma separated entry in rails form

  6. 6

    Rails create multiple records at once from different models

  7. 7

    How to find index of array of records return from database in rails?

  8. 8

    rails first_or_create for multiple records

  9. 9

    Rails Create Multiple Records in Join Table

  10. 10

    Create multiple records with a simple form on Rails

  11. 11

    Create multiple records with a simple form on Rails

  12. 12

    Create multiple records in rails with collection_select

  13. 13

    How to create multiple arrays from one array?

  14. 14

    How to create multiple SparkContexts in a console

  15. 15

    Get all Rails records from an array of IDs

  16. 16

    How to create a where clause that returns no records in Rails?

  17. 17

    How to create a where clause that returns no records in Rails?

  18. 18

    create single field array from all the records

  19. 19

    Rails Activerecord: How to exclude records with multiple associated records

  20. 20

    Inserting multiple records into MySQL from an array

  21. 21

    How to get the count of all records of a model in Rails console

  22. 22

    How to get the count of all records of a model in Rails console

  23. 23

    Ruby on Rails: create records for multiple models with one form and one submit

  24. 24

    Viewing Nested Records in Rails Console

  25. 25

    How to create route to array in rails

  26. 26

    how to create multiple checkbox from associative array in angular js

  27. 27

    How to Store Multiple records in My Array List from Azure mobile service Using API Code?

  28. 28

    Rails - Unable to create records from csv file on VPS

  29. 29

    How to display associations from rails console

HotTag

Archive