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 Value Using Proc #140

Open
jiunjiun opened this issue Jan 4, 2024 · 1 comment
Open

Default Value Using Proc #140

jiunjiun opened this issue Jan 4, 2024 · 1 comment

Comments

@jiunjiun
Copy link

jiunjiun commented Jan 4, 2024

I've been developing using the default value with proc in Kredis and encountered an issue.

However, I'm not sure if there's a misunderstanding in my use of the design.

app/models/user.rb
class User < ApplicationRecord
  kredis_string :full_name, default: ->(u) { "#{u.first_name} #{u.last_name}" }
end

user1 = User.create(first_name: 'John', last_name: 'Doe')
user2 = User.create(first_name: 'Tom', last_name: 'Smith')

I expected it to work like this:

user1.full_name # => 'John Doe'
user2.full_name # => 'Tom Smith'

But what actually happens is:

user1.full_name # => 'John Doe'
user2.full_name # => 'John Doe'

The default proc for the user seems to retrieve the data of the first user record.

Upon further investigation, it seems related to this segment of the code:

when Proc then Proc.new { default.call(self) }

May I ask if this is considered an issue?

@danielnc
Copy link

I am also facing the same issue, so instead of having a default proc i need to manually add the first value in cache if it doesn't exists using a redis transaction

just want to know if this is intended behavior or a bug

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

No branches or pull requests

2 participants