Rails 6.1 adds ActiveRecord::FinderMethods 'sole' and 'find_sole_by' | BigBinary Blog

There were no methods defined to find and assert the presence of exactly one record at the same time.For example, we have a class Product with a price field and we want to find a single product that has a price of 100. For zero or multiple products with the price of 100, we want to raise an error. We can not add database constraints to make a unique field of price.Now to solve the above query, we don't have any method defined in ActiveRecord::FinderMethods module. We can find a product with the given price or raise an error if no record is found using the queries mentioned in the below example.
Rails 6.1 adds ActiveRecord::FinderMethods 'sole' and 'find_sole_by' | BigBinary Blog #ruby #rubydeveloper #rubyonrails #activerecord::findermethods #bigbinary #blog https://rubyonrails.ba/single/rails-6-1-adds-activerecord-findermethods-sole-and-find_sole_by-bigbinary-blog

Nezir Zahirovic

Contractor Ruby On Rails (8+ years) / MCPD .Net / C# / Asp.Net / CSS / SQL / (11 years)

related articles