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

Empty string given as the foreign key ID for an aggregate_belongs_to attribute ends up storing the ID as 0 #147

Open
omeedrj opened this issue Oct 22, 2021 · 1 comment
Labels

Comments

@omeedrj
Copy link
Contributor

omeedrj commented Oct 22, 2021

Description

id_or_value.to_i within Aggregate::ForeignKeyReference#initialize can cause an empty string given as an id to be stored as 0. This 0 will then be stored as the aggregate_belongs_to attribute's ID in serialized storage data. This means that trying to fetch the associated DB record will (most likely always) raise an exception because the record won't be found, which ends up becoming confusing to debug because it's not necessarily clear to the consumer of the gem why 0 is being stored as the ID.

@id = id_or_value.to_i

Example

aggregate_belongs_to :user, "User"
my_object.user_id = ""
my_object.save!

my_object.user_id == 0

Suggested Fix

  • When id_or_value is not an ActiveRecord::Base object, we should raise an ArgumentError if id_or_value is nil or an empty string
@omeedrj omeedrj added the bug label Oct 22, 2021
@omeedrj omeedrj changed the title Giving an empty string as the foreign key ID for an aggregate_belongs_to attribute stores the ID as 0 Empty string given as the foreign key ID for an aggregate_belongs_to attribute ends up storing the ID as 0 Oct 22, 2021
@dep
Copy link

dep commented Oct 22, 2021

Discovered during the creation of this invoca/web PR. Linking for posterity.

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

No branches or pull requests

2 participants