Problem with Active Record
Posted
by kshchepelin
on Stack Overflow
See other posts from Stack Overflow
or by kshchepelin
Published on 2010-03-25T12:35:54Z
Indexed on
2010/03/25
12:43 UTC
Read the original article
Hit count: 291
ruby-on-rails
|activerecord
Hello everyone.
Lets assume we have a User model. And user can plan some activities. The number of types of activities is about 50. All activities have common properties, such as start_time, end_time, user_id, etc. But each of them has some unique properties.
Now we have each activity living in its own table in DB. And thats why we have such terrible sql queries like
SELECT * FROM `first_activities_table` WHERE (`first_activity`.`id` IN (17,18))
SELECT * FROM `second_activities_table` WHERE (`second_activity`.`id` = 17)
.....
SELECT * FROM `n_activities_table` WHERE (`n_activity`.`id` = 44)
About 50 queries. That's terrible.
There are different ways to solve this.
- Choose the activity type with the biggest number of properties, create the table 'Activities' and have STI model. But this way we must name our columns in uncomfortable way and often the record in that table would have some NULL fields.
- Also STI model, but having columns, common for all of activity types and some blob column with serialized properties. But we have to do some search on activities - there can be a problem. And serialization is quite slow.
Please help me dealing with this. Maybe my problem has quite different solution that will fit my needs.
Thanks for help.
© Stack Overflow or respective owner