Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass object or id

This is just a question about best practices.

Imagine you have a method that takes one parameter. This parameter is the id of an object. Ideally, I would like to be able to pass either the object's id directly, or just the object itself.

What is the most elegant way to do this?

I came up with the following:

def method_name object
  object_id = object.to_param.to_i
  ### do whatever needs to be done with that object_id
end

So, if the parameter already is an id, it just pretty much stays the same; if it's an object, it gets its id.

This works, but I feel like this could be better. Also, to_param returns a string, which could in some cases return a "real" string (i.e. "string" instead of "2"), hence returning 0 upon calling to_i on it. This could happen, for example, when using the friendly id gem for classes.

Active record offers the same functionality. It doesn't matter if you say:

Table.where(user_id: User.first.id) # pass in id

or

Table.where(user_id: User.first) # pass in object and infer id

How do they do it? What is the best approach to achieve this effect?

like image 961
weltschmerz Avatar asked Nov 05 '13 11:11

weltschmerz


People also ask

Can we pass object in params?

When you put an object variable name as a parameter to a method in Java, you are passing a 'pointer to' that object. You can think of it as the object being represented by its address in memory, and that you are passing a variable that holds that address.

How do I find the ID of an object?

Find User (Object ID) Browse to or search for the desired user, then select the account name to view the user account's profile information. The Object ID is located in the Identity section on the right. Find role assignments by selecting Access control (IAM) in the left menu, then Role assignments.

How do you pass an object in C#?

"Objects" are NEVER passed in C# -- "objects" are not values in the language. The only types in the language are primitive types, struct types, etc. and reference types.

Can you pass an object into a function?

An object can be passed to a function just like we pass structure to a function. Here in class A we have a function disp() in which we are passing the object of class A . Similarly we can pass the object of another class to a function of different class.


1 Answers

If the process is cross controller actions or in session, better to use id. For example, you are going to save a cart in session, the better choice is id. It's hard to watch how big an object is, using id will help performance and avoid unnecessary error.

However, if the methods are inside same request and all actions are within memory, using object itself would be quicker. For example, you pass an user to an authority class to check if he can do something. Because all objects are just a reference in memory, extra step to extract id is unnecessary and inefficient.

like image 61
Billy Chan Avatar answered Sep 17 '22 22:09

Billy Chan