Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling jsonb column doesn’t return default values #175

Open
borisrorsvort opened this issue Dec 6, 2023 · 4 comments
Open

Calling jsonb column doesn’t return default values #175

borisrorsvort opened this issue Dec 6, 2023 · 4 comments

Comments

@borisrorsvort
Copy link

Given this configuration

  jsonb_accessor :voting_options, voting_options_type: [:string, { default: "single", store_key: :type }] 

With an empty db column i get an empty hash

irb(main):015:0> a.voting_options
=> {}
irb(main):016:0> a.voting_options_type
=> "single"
irb(main):017:0> a.voting_options["type"]
=> nil
irb(main):018:0>

I would be expecting to get the default values

{
    "type": "single"
}
@haffla
Copy link
Contributor

haffla commented Dec 6, 2023

jsonb_accessor :voting_options, voting_options_type: [:string, { default: "single", store_key: :type }]

means that the default for the accessor voting_options_type should be "single".

@borisrorsvort
Copy link
Author

@haffla exactly.

Though we noticed that the bug does not occur when creating a new object. In our case:

a  = Post::IdeasRequest.new
a.voting_options  #> {"type"=>"single"}

This only happens if the column default value is {}, nil or when only one value has been set manually after.

example:

Given this other config

jsonb_accessor :voting_options,
                 voting_options_type:        [:string, { default: "single", store_key: :type }],
                 voting_options_budget_rule:   [:boolean, { default: false, store_key: :budget_rule }]

When assigning only one manually:

irb(main):039:0> a = Post::IdeasRequest.new
=>
#<Post::IdeasRequest:0x000000010e06bf58
...
irb(main):040:0> a.voting_options
=> {"type"=>"double", "budget_rule"=>false}
irb(main):041:0> a.voting_options = {}
=> {}
irb(main):042:0> a.voting_options_type = "single"
=> "single"
irb(main):043:0> a.voting_options
=> {"type"=>"single"}
irb(main):044:0>

While i’m expecting to always have at least all the defined keys in the returned values when calling voting_options.

@haffla
Copy link
Contributor

haffla commented Dec 6, 2023

I think this gem wasn't developed with using the jsonb field directly in mind. In your case voting_options. But rather like

idea_request.voting_options_type = "something"
idea_request.voting_options_budget_rule = "another thing"
idea_request.save!

Basically you can store anything in voting_options and use "accessors" to access fields in the hash.

I also have to say that I understand your report, though I am almost 100% sure that there was someone in the past who opened an issue because the default values are saved to the database. So I am not sure if this here is a bug report or feature request. It could be something configurable. Some people prefer it this way and other that way.

@haffla
Copy link
Contributor

haffla commented Dec 6, 2023

On another note. I would be in favour for your suggested behaviour and would accept a PR. I don't have capacity to work on it myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants