I have application with 2 groups of models - content based (news, questions) and "something" based (devices, applications etc). I need to link all models between groups - for example question may belongs to 3 different things - one application and 2 devices. The same - for news. From other side - i need to see all news articles and questions related to some application or device.
Any idea how to develop this in rails? I have only one idea - mi开发者_StackOverflow中文版xins that will add methods content_id and thing_id to models and join table.
You could use thing_id, thing_class, content_id, content_class only in join table and search with conditions, habtm would be imposible. "2, 'Devise', 'Page', 2" "2, 'Devise', 'Advertisement', 4" "2, 'AnotherThing', 'Page', 2"
And then fetch all pages for thing from join table where id = self.id AND thing_class = self.class.name AND contant_class = Page.class.name
If you need to use habtm association, you could use STI + 1:1 association, but that will mess the things even more
Solved with has_many_polymorphs plugin - http://github.com/fauna/has_many_polymorphs
精彩评论