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

How to persist default attributes? #106

Open
dpaluy opened this issue Nov 8, 2021 · 1 comment
Open

How to persist default attributes? #106

dpaluy opened this issue Nov 8, 2021 · 1 comment

Comments

@dpaluy
Copy link

dpaluy commented Nov 8, 2021

By default, store_model isn't persisted in DB, unless I change an attribute explicitly.

What is the correct way to store default attributes in the DB?

I have the following model:

class User < ApplicationRecord
  attribute :email_settings, Subscriptions.to_type
  
   scope :weekly_subscribed, -> { where('email_settings @> ?', { weekly: true }.to_json) }
end

 class Subscriptions
    include StoreModel::Model

    attribute :weekly, :boolean, default: true
end

When a new user is created, the email_settings value in the DB is {}.

But I want to query all users using jsonb query, as following: User.weekly_subscribed

puts User.weekly_subscribed.count
# 0

Because the JSONB doesn't have a default value in the DB.

Gem: 0.12.0
Ruby: 2.7.4
Rails: 5.2.5

@DmitryTsepelev
Copy link
Owner

Hi @dpaluy! Not sure if it's possible in the Attributes API, but you definitely can set the default value in the database itself

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