drew578
drew578

Reputation: 131

rails form fields_for serialize hash

I need to create a check list for a comment form on which users to notify about a comment.

I have a comment model and i have a column called comment_meta which i want to store as a serialize hash.

My comment form has a fields_for comment_meta example;

<%= f.fields_for :comment_meta do |comment_form| %>
<%= comment_form.check_box(contact.id) %>
<% end %>

The params being passed are "comment_meta"=>{"155"=>"0", "156"=>"1", "157"=>"0"}}, but my db is saving an empty hash.

The field type for comment_meta is a text.

Is there a way to save this?

Upvotes: 0

Views: 923

Answers (1)

mkralla11
mkralla11

Reputation: 1299

Your comment_meta hash seems to be a 'key-value pair' of ids and Boolean values, respectively. If this is true, then you may be better off going with a users_comment_metas 'through' table.

You would benefit from restructuring to this methodology because:

  1. db indexing is fast - rails hash serialization/parsing to and from the db is slow (unless your using postgres's h-store, but thats another story)
  2. Dynamic reporting about all data in your db will be much more intuitive/query-able as a developer attempting to display readable reports (which is very important at my job at least).
  3. Your goal of using Rails' built in form helpers will be much easier.

If you truly want to go the hash route you've suggested, there could be a variety of things preventing it from saving. Here's how I would troubleshoot:

  • First make sure you have the line 'serialize comment_meta, Hash' in your Comment model.
  • Remove ALL validations you may have in the Comment model for testing purposes.
  • Now, type 'debugger' (no quotes) in your controller create and/or update action in your comments_controller (or the controller you are requesting from the form)
  • when you hit your debugger statement in your terminal, type the words 'eval comment_params' to check the comment params you've submitted (I am assuming you are using the strong parameters gem, seeing as you typed 'ruby-on-rails-4' in your tags for this post).

  • The hash should have this structure as you've suggested above (if it looks different, something is wrong in your form, or you have strong paramters set up incorrectly, more info here):

"comment_meta"=>{"155"=>"0", "156"=>"1", "157"=>"0"}

  • type 'n' until you've passed the line initializing a new object with the params

    @comment = Comment.new(comment_params)

  • Now type 'eval @comment' to see your new object, and then type 'eval @comment.comment_meta' to see if the hash is being stored. If it's returning nil, then type 'eval @comment.valid?'. If it returns true, then type @comment.errors to see what is wrong, and fix accordingly.

    If all else fails, make sure you can do a manual assignment of any given hash to comment_meta of a new Comment object in your rails console:

    @comment = Comment.new() @comment.comment_meta = {"test"=>"1"} @comment.save!

I hope this gets you on the right track.

Upvotes: 1

Related Questions