Incidentally, apart from this precise case, I have never been able to decide when it is better to pass ActiveRecord an id, as in .where shelf_id: shelf.id, or the entire object .where shelf: shelf.
The second example seems more idiomatic, but I never knew when to favor one or the other.
By the way, I used the method where as an example, but it could be any other method that accepts an ActiveRecord instance or an id.
Children's museum worker turned software engineer with a lot of hard work and a great bootcamp experience. Currently making good things great at Untappd.
Totally agree! It's the one mystery of this change that I really want to investigate - because it's just not clear to me which to use when - and you're right, it's a decision we have to make in many other instances.
In most other ORM’s, in other frameworks, it is common to change those relationships using the entire object and not just setting the foreign key relationship with a primary key of a parent object. Clearly, rails has decided to go down that same avenue and it makes sense.
A proper framework should result in identically performing SQL whether you were specific in .where shelf_id: = shelf.id
or
.where shelf:=shelf
In the second case, the framework would understand that the primary key of shelf is ‘id’ and only utilize that one column in the query.
I love creating! It started with Lego as a little kid. Later I went on with (dis)assembling my first computer in the early 2000s. Then came the internet... Working remotely for 8 years :-)
In an object oriented world, I believe we should pass objects around, not properties of objects. I guess that's what you mean with "idiomatic" already. Of course there are exceptions, e.g. when passing it to an asynchronously called worker, to avoid problems when dumping the payload into Redis as a json value, but usually you instantiate the object in said worker in the first line, to continue working with, well, objects.
Besides this, thank you for sharing your experience in this article, you're for sure not the only one running into this problem :-)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thanks for the insight!
Incidentally, apart from this precise case, I have never been able to decide when it is better to pass ActiveRecord an
id
, as in.where shelf_id: shelf.id
, or the entire object.where shelf: shelf
.The second example seems more idiomatic, but I never knew when to favor one or the other.
By the way, I used the method
where
as an example, but it could be any other method that accepts an ActiveRecord instance or anid
.Totally agree! It's the one mystery of this change that I really want to investigate - because it's just not clear to me which to use when - and you're right, it's a decision we have to make in many other instances.
In most other ORM’s, in other frameworks, it is common to change those relationships using the entire object and not just setting the foreign key relationship with a primary key of a parent object. Clearly, rails has decided to go down that same avenue and it makes sense.
A proper framework should result in identically performing SQL whether you were specific in .where shelf_id: = shelf.id
or
.where shelf:=shelf
In the second case, the framework would understand that the primary key of shelf is ‘id’ and only utilize that one column in the query.
In an object oriented world, I believe we should pass objects around, not properties of objects. I guess that's what you mean with "idiomatic" already. Of course there are exceptions, e.g. when passing it to an asynchronously called worker, to avoid problems when dumping the payload into Redis as a json value, but usually you instantiate the object in said worker in the first line, to continue working with, well, objects.
Besides this, thank you for sharing your experience in this article, you're for sure not the only one running into this problem :-)