Gozup
Gozup

Reputation: 1003

rails serialized association instead of has_many through

Can I serialize an association? I mean, I have a Question model that has_many Interests :through => :interests_questions. Questions and interests tables are huge and I don't want to make their size increase. So, is there a way to make interests_questions table with columns question_id and interests_ids where interest_ids receive a serialized Hash containing all associated interests?

Does activeRecord handle this kind of stuff or is there a Gem?

Thank you

Upvotes: 2

Views: 1314

Answers (1)

moritz
moritz

Reputation: 25757

ActiveRecord doesn't do that. Mostly because of the fact that this is not a classical approach for RDBM systems. The problem is that you could really only see all interests for one or more questions and that would immediately involve two queries, because you can't join or includes. Additionally, you can't run (db) queries that depend on the relationship between questions and interests. For example asking for all interests that have been created less than a week ago and belong to questions 5, 9, 12 could only be done by iterating all interests of those questions in ruby.

You could have a look at Mongoid for MongoDB. It does exactly that. It could even embed all interests entirely within a question.

Upvotes: 3

Related Questions