madeintandem / jsonb_accessor

Adds typed jsonb backed fields to your ActiveRecord models.
MIT License
1.11k stars 93 forks source link

Get unknown field error when trying to parse params #139

Closed TTD93 closed 2 years ago

TTD93 commented 2 years ago

We've done the following:

# migration
class AddDataToPosts < ActiveRecord::Migration[6.1]
  def change
    add_column :posts, :data, :jsonb, default: {}
  end
end

# model: 
class Post < ApplicationRecord
  jsonb_accessor :data,
                 body:                     :string,
                 sending_method: string
end

Gem version 1.3.2 Rails version 6.1.4 Ruby version 2.6.7

When I try to do the following:

params =  params.permit(:body, :sending_method)
Post.new(params)

I get the error message UNHANDLED ERROR unknown attribute 'body' for Post.

This is the param: #<ActionController::Parameters { "sending_method"=>"email", "body"=>"<div>Fake body</div>"} permitted: true>

However, when I do this, it works

new_post = Post.new
new_post.body = "fake body string"
new_post.save!" 
TTD93 commented 2 years ago

Sorry, nevermind. Due to how rails load classes I had to restart the server for the attr to take effect!