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

ActiveRecord's exists? method always return false #85

Open
Antronin opened this issue Jun 16, 2024 · 0 comments
Open

ActiveRecord's exists? method always return false #85

Antronin opened this issue Jun 16, 2024 · 0 comments

Comments

@Antronin
Copy link

Antronin commented Jun 16, 2024

Rails ActiveRecord has a method called exists? which returns a boolean true if the record exists or false if not.
This method now always returns ```false` ' if I give it a hashid instead of a normal ID.

Example code:

context.cart[:items] = context.cart[:items].select do |cart_item_hashid|
    Shop::Item.exists?(cart_item_hashid) # always false, Cart is emptied
  end

It should generate the following SQL:

SELECT 1 AS one FROM `shop_items` WHERE `shop_items`.`id` = 14 LIMIT 1

The current solution I'm using:

context.cart[:items] = context.cart[:items].select do |cart_item_hashid|
  Shop::Item.find(cart_item_hashid).present?
end

This generates the following SQL:

SELECT `shop_items`.* FROM `shop_items` WHERE `shop_items`.`id` = 14 LIMIT 1

This returns the whole record from the DB to check in Ruby code that it is not ```nil` '; it would be much more efficient just to return a boolean.

Another workaround is to do the following:

context.cart[:items] = context.cart[:items].select do |cart_item_hashid|
    Shop::Item.exists?(Shop::Item.decode_id(cart_item_hashid))
  end

It works fine; however, it is a bit redundant.

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

1 participant