Skip to content

ActiveRecord's exists? method always return false #85

Open
@Antronin

Description

@Antronin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions