Skip to content

Default Value Using Proc #140

Open
Open
@jiunjiun

Description

@jiunjiun

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions