-
Notifications
You must be signed in to change notification settings - Fork 1
Polymorphic associations
Just like regular associations, you can declare polymorphic ones. Polymorphic means that associated object can reference any number of tables (models), by storing not only owner object id, but also owner object table name. Polymorphic association has association name, which is used to specify the relationship.
For example, we want to create Comment model, that could associate comments with both orders and users (and whatever else we will need to comment later on). To do it, we need to create comments
table that specifies two reference columns. Lets name our relationship as commentable, then the table migth look like:
create table comments(
id serial primary key,
commentable_id integer not null,
commentable_type varchar(64),
text TEXT
);
Now all we need is to specify polymrphic assotiation whenever we need it:
class Comment extends prego.Table
# ....
class Order extends prego.Table
@hasMany Comment, { as: 'commentable' }
class Person extends prego.Table
@hasMany Comment, { as: 'commentable' }
Now we can add comments to both tables the same way as with plain associations:
order.comments_create { text: 'things are great }, (err) ->
# err || comment saved
c = person.comments_build
c.text = 'foobar'
c.save
All other functions run as expected, too. The only difference is that ploymorphic association uses association name to get owner object that might be of arbitrary table/class, e.g.
object = c.commentable
# Object might be Order or Person instance in our sample