I have many different association settings in Rails with ActiveRecord. Let's say tables
+------------+ +----------------+ +----------+ | categories | -- has many -= | category_items | =- has many -- | products | +------------+ +----------------+ +----------+
In the category_items table, I have a standard id set plus an additional attribute called "type":
id:int category_id:int product_id:int category_type:string
Fortunately, Rails provides great helpers for creating bindings in the mapping table. For instance:
p = Product.first
This is all good and good, but let me say that I want to automatically update the type field with the class name of another table. Therefore, for the above examples, the category_items line would look like this:
id = 1 (or some number) category_id = 1 product_id = 1 category_type = nil
But I would like category_type to equal "Product". Is there a way for me to build a callback or define something in the association that automatically set the category_type field? I know that in polymorphic associations you can use something like: source or: to do something similar, but polymorphic associations in many, many associations cause an error.
Any ideas?
Thanks!
source share