Closed danelowe closed 9 years ago
@danelowe are you after something like this in AR?
post = Post.new
post.comments << Comment.new
post.save
@jsmestad Yes, exactly. To be honest I just wondered if it was supported, and my investigation took me down a rabbit hole.
@danelowe I like the functionality however having written that sort of code before it could have some interesting race conditions. Think the convenience is worth the effort?
@jsmestad When you put it that way, probably not :)
I'm keen for feedback on this, and whether it is a good idea. I understand it is probably quite a considerable change. Particularly let me know if you're comfortable with the change in behaviour represented by the change in the specs.
There's definitely more refactoring that could be done, but would like some feedback beforehand.
Basically, I wished for the ability to 'push' a new record onto a has-many association. The reading and writing to an association would have different behaviour depending on the type of the association, so I thought that it might be a good place to have different objects managing the behaviour.
I've tried to make sure as much of the logic as possible is retained in each instance of an association, and have the class simply retain the
_association_options
hash.Also, I just looked at the ResourceMethods.attach method I'd made. I am now wondering if the methods it dynamically defines would be retained the next time the same module is used, or if each module is its own instance. Having it retain its methods between calls would not be good. I might have to do a bit of reading up on Ruby modules.