1

I'm using selectize gem for this task and can't understand how to add values without {} to those would be saved correctly. I have array: true on this column, so values must be sent in an array, not the string.

I tried convert this attribute before saving of record, but still no result.

class Post < ActiveRecord::Base
  before_validation :convert_to_array

  def convert_to_array
    self.tags = tags.split(",")
  end
end

Error

ERROR:  malformed array literal: "{{}}"

If there aren't callbacks

    Parameters: {"post"=>{"title"=>"Aliquam laborum consequatur voluptatem quo.",
"tags"=>"asdas,zxzx"}, "_wysihtml5_mode"=>"1", "commit"=>"Save", "id"=>"4"}
4
  • show the migration of the model ...Post ,, for the column tags. Commented Aug 23, 2015 at 13:38
  • t.string :tags, array: true Commented Aug 23, 2015 at 13:48
  • from where you are getting the value of tags here tags.split Commented Aug 23, 2015 at 13:51
  • can you override the setter method for tags attribute and set the tag after splitting.? Commented Aug 23, 2015 at 13:58

1 Answer 1

1

You can do this in the controller (because params is not available in your model):

tags = params['post']['tags'].split(',')
@post = Post.new
@post.update_attributes(tags: tags)
Sign up to request clarification or add additional context in comments.

4 Comments

Yes.. I have the same doubt.
Yeah, params is not available to the model.
@post.update_attribute(:tags, tags) I guess. Thank you, it works for me.
Both should work. I used update_attributes because it will run the validation. Whereas update_attribute doesn't validate the record.

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.