Rails: Many to many polymorphic relationships

前端 未结 7 2068
孤城傲影
孤城傲影 2021-01-30 00:17

See comments for updates.

I\'ve been struggling to get a clear and straight-forward answer on this one, I\'m hoping this time I\'ll get it! :D I definitely have

7条回答
  •  天涯浪人
    2021-01-30 00:53

    I agree with the others I would go for a solution that uses a mixture of STI and delegation would be much easier to implement.

    At the heart of your problem is where to store a record of all the subclasses of Target. ActiveRecord chooses the database via the STI model.

    You could store them in a class variable in the Target and use the inherited callback to add new ones to it. Then you can dynamically generate the code you'll need from the contents of that array and leverage method_missing.

提交回复
热议问题