Polymorphic association without type Rails (unique identifier)

In my application, I have a unique identifier for each object (table).

Now because of this, when I see the identifier, I know what type of object it is, whether it is a User or his hotel.

I was wondering if I can save the search for item_type in polymorphic associations, fix the search by searching for the sequence in memory, thereby saving space in the database and in the index.

Can this be done?

I work with Rails 3.0.9, Ruby 1.9.2

+8
activerecord ruby-on-rails-3
source share
2 answers

This is not the answer you are looking for. I'm sure this is possible somehow, but you will fight against the grain of how polymorphism was developed in ActiveRecord, and this is likely to cause a load.

The first question that comes to me is why? You are asking for performance optimization. Do you see a performance problem? Have you checked with the toolkit, New Relic, the Ruby profiler, and other tools that this second search is really what kills your performance? If you have not done so, you are probably wasting your time. Predicting performance bottlenecks is an inaccurate science and follows rule 80-20.

If you really have this problem, and you carefully analyzed your logs, your new relic diagrams, you isolated the problem and ran performance tests against it, if you did all this and you "I see a performance problem with this problem, then I would suggested that the denormalization sort solution is likely to give you some improvement.Denormalization is a common tool for optimizing database performance problems.You will store data in more than one place, but your request Wasps will touch fewer tables (faster) with additional overhead while maintaining synchronization of several bits when updating records (more complex application code).

If you could post more details about your example, it would be easier to make some more specific suggestions or provide examples.

+1
source share

I think this article may be useful for you:

Polymorphic associations in Rails using xid (unique throughout the system) http://blog.mustmodify.com/2009/07/polymorphic-associations-in-rails-using.html

+1
source share

All Articles