Coder Social home page Coder Social logo

Comments (4)

DmitryTsepelev avatar DmitryTsepelev commented on May 24, 2024 1

I'm closing this, please let me know if you need any help here, and I'll reopen it 🙂

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 24, 2024

Hi @joshRpowell! Thanks for the report, I'll do my best to take a look at the issue later this week

from store_model.

DmitryTsepelev avatar DmitryTsepelev commented on May 24, 2024

@joshRpowell I was able to reproduce it! The problem is that in schema you set up "{}" as a default, and then change default to {} in the attribute declaration. When replacing :jsonb with store models you removed attribute-level override, so the app crashes, when at least one jsonb field gets default string value. There are two options you have:

  1. bring back defaults in attributes definition
  2. change schema to use {} instead of "{}"

Here is a full example (save it to file, run createdb railstestdb and then execute the file with Ruby):

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"

  gem "rails"
  gem "pg"
  gem "factory_bot_rails"
  gem "store_model"
end

require "active_record"
require "action_controller/railtie"

ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "railstestdb")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  enable_extension "plpgsql"

  create_table "worker_options", id: :serial, force: :cascade do |t|
    t.jsonb "options", default: "{}", null: false
    t.jsonb "data", default: "{}", null: false
    # Option 1:
    # t.jsonb "options", default: {}, null: false
    # t.jsonb "data", default: {}, null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
end

class WoCases
  include StoreModel::Model

  attribute :uri, :string
end

class Options
  include StoreModel::Model

  attribute :id, :integer
  attribute :number, :string
  attribute :receipt_number, :string
end

class WorkerOption < ActiveRecord::Base
  attribute :data, WoCases.to_type
  attribute :options, Options.to_type

  # Option 2:
  # attribute :data, WoCases.to_type, default: {}
  # attribute :options, Options.to_type, default: {}
end

FactoryBot.define do
  factory :worker_option, class: 'WorkerOption' do
    data { { "uri" => 'http://localhost:3000/api/v1/work_flows/1/case_machines/1/cases' } }

    trait :valid_options do
      options { { "id" => 666, "number" => "NUMBER", "receipt_number" => "RECEIPT_NUMBER" } }
    end
  end
end

puts FactoryBot.create(:worker_option).inspect
# The following line always works, because all the attributes are set up!
puts FactoryBot.create(:worker_option, :valid_options).inspect

from store_model.

joshRpowell avatar joshRpowell commented on May 24, 2024

@DmitryTsepelev Thanks for the prompt response. I made the recommended adjustments and it resolved my issues.

from store_model.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.