2

I have a text field in a form for adding tags to a category. I want the input to be a comma separated string like some,test,tags and have it save that as an array in posgresSQL. My tags column was created with t.string "tags", array: true.

In my controller I have

category_params[:tags].split(',')
@category = Category.create(category_params)

But when I enter some,test,tags it shows up in the database as ["ome", "test"] What have I done wrong here?

7
  • what is the expected output? Commented Dec 5, 2016 at 2:07
  • ["some","test","tags"] Commented Dec 5, 2016 at 2:08
  • Have you tested it without this array: true? It seems to me this is your problem. You have a common string field comma separated. I think you don't need this. Commented Dec 5, 2016 at 2:08
  • 2
    You realize category_params[:tags].split(',') doesn't mutate the value right? Does Category.create(category_params.merge(tags: params[:tags].split(','))) work as expected? Commented Dec 5, 2016 at 2:32
  • 1
    @etdev That was the problem Category.create(category_params.merge(tags: params[:tags].split(','))) didn't work but I did something similar that did work. Commented Dec 5, 2016 at 2:55

1 Answer 1

2

Got it working with

tags = category_params[:tags].split(',')
Category.create(category_params.merge({tags:tags}))
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.