Polymorphic index? No problems!
If you use thinking-sphinx, this is a nice trick if you need to index polymorphic attributes.
In our case, we have questions that could belong to campaigns or startups. So those questions are polymorphic. But we still need to index them. Here is the description of the index in pure SQL:
indexes "CASE questions.subject_type
WHEN 'Campaign' THEN (
SELECT campaigns.name
FROM campaigns
WHERE campaigns.id = questions.subject_id
)
WHEN 'Startup' THEN (
SELECT startups.name
FROM startups
WHERE startups.id = questions.subject_id
)
END", :as => :startup
````
Thanks [@pedrocarrico](twitter.com/pedrocarrico)!
Written by Pierre Ozoux
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#