I am a seasoned developer with over 9 years of experience in Ruby on Rails and Golang. My passion lies in tackling complex data structures and algorithm problems, always pushing myself to learn and grow. In my downtime, I find solace in the pages of books and the soothing sounds of a guitar. But I'm not just a one-trick pony! I love to stay active and challenge myself in other ways too. Whether it's on the football pitch, cricket field, table tennis table, or badminton court, I'm always up for some fun activities.
1 minute read
Rails 6.1 has added a feature wherein we can
specify an alias name of the model in the where clause.
Let’s say we have an Employee model as below:
Employees without a manager will have manager_id as nil.
Before
Let’s take a scenario where we need to fetch all employees whose manager name is Sam.
There can be two ways to fetch this data.
The second query throws an error of missing FROM-clause entry.
It expects manager to reference the employees table, which Rails was not able to figure out.
After
To fix the above issue, Rails 6.1.1 added a fix that allows the alias name to be used
as a reference for the employees table in the where clause.
This use-case is mostly useful for self-referential models and aliased tables.