I’m wondering what the best practices/conventions are for passing
parameters to models I am writing. Is it better to pass ActiveRecord
objects, or their database IDs?
In my current application, I’ve been passing mostly ActiveRecord
objects, because I’m generally using some attributes of that object.
Passing the ID and then doing a find all over again seems like a waste.
On the other hand, I’m finding that most of the code that I’m pushing
out of the controllers into my model is likely to have IDs, since users
are often submitting forms with the object IDs. In those cases, I’m
doing an extra find in the controller to get the object to pass to the
model, which in many cases is simply doing a DB call using the ID as a
So, my question is, what is recommended by the experts?
- Pass only ActiveRecord objects
- Pass only database IDs
- Pass whatever is more efficient for the DB in the particular use case
(this seems like a headache for overall application consistency)