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

Default values for attributes when retrieving the store model #132

Open
raivil opened this issue Nov 9, 2022 · 1 comment
Open

Default values for attributes when retrieving the store model #132

raivil opened this issue Nov 9, 2022 · 1 comment

Comments

@raivil
Copy link

raivil commented Nov 9, 2022

Hey,
I'm trying to configure default values for attributes but I'm not sure if I'm doing something wrong or it's something not supported/bug.

Defining "default" will return the default value only when instantiating the store model, but not when retrieving an attribute

here's a simplified example that shows the behavior.

class Settings < ApplicationRecord
   attribute :colors, Colors.to_type
   validates :colors, store_model: true, allow_blank: true 
end

class Settings::Colors
  include StoreModel::Model

  attribute :bg_default, :string, default: "#ff0000"
  attribute :bg_overlay, :string, default: "#a6af1d"
end

irb(main):040:0> Settings.last.colors                                                                        
=> #<Settings::Colors bg_default: nil, bg_overlay: nil,

irb(main):041:0> Settings.new.colors
=> #<Settings::Colors bg_default: #ff0000, bg_overlay: #a6af1d,

irb(main):042:0> Settings.new.colors.bg_default
=> "#ff0000"
irb(main):043:0> Settings.last.colors.bg_default
=> nil 
@DmitryTsepelev
Copy link
Owner

Hi @raivil! This is how attributes API works, you have to add the default to the database to make it work

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