When I make a new object, let's say
o = Object.new
This object has an id,
o.object_id
#=> ########
I also make several other objects, using the Object class. What would be the best way to have ruby find the object 'o' by using the object_id attribute? I am thinking in terms of something like
search_id = o.object_id
search_result = Object.find(search_id)
Where 'search_results' would be the object corresponding to 'search_id'. Also, I would definitely appreciate an altogether different approach to indexing objects and retrieving them by a guid or something. Thanks so much!
Hah, well I guess I really just need to think about this in the context of a database and just use MySQL queries or those of whatever DB I choose to find the object. The more I think about it, the only possible things that would be accessible through this imaginary 'find()' method would be things that are newly created or 'active'? Sorry for making this a crappy question.
Yes, you can:
Should you do this? I'll leave that up to you. There are less geeky ways to store an object with a serializable
id
(e.g. in an Array and returning the index). One problem you may run into is that if the only 'reference' you keep to an object is theobject_id
, the object can be collected by GC when you're not looking.