build method for specific models?
JasonBarnabe opened this issue · 2 comments
JasonBarnabe commented
In the examples:
store = Store.create
store.products << Pen.create
store.products.first
# => #<Product: ...>
But this saves the Pen
on line 2. What if I want to attach but not yet save it, as I want to add additional attributes? The <<
method will save whatever you pass to it.
In the normal, non-inheritence world, I would do:
store = Store.create
product = store.products.build
# ...
product.save
But if I'm using inheritence, if I want it to be a Pen
, I've found this works:
store = Store.create
product = store.products.build(actable: Pen.new)
# ...
product.save
Maybe there is already a better way. If not, I would propose something like this:
store = Store.create
product = store.products.build_pen
# ...
product.save
manuelmeurer commented
Sure, I'd be open to adding something like that!
Want to work on a PR?
manuelmeurer commented
Closing due to inactivity.