1

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

2
  • you can call .new method only on class and class should start with capital `Category Commented Jan 2, 2015 at 23:31
  • you should consider using factorygirl btw ;) Commented Jan 2, 2015 at 23:47

1 Answer 1

2

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)}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.