I have a item tracking system which has an Item model which has a one to many relationship with an Invoice. Meaning that one invoice can have many items and an item can belong to one invoice.
I've now run into the need to return an Item into stock which for my system means setting the delivered boolean field back to false, but unless I set invo开发者_运维百科ice_id to nil it will still show up as awaiting delivery to a customer and if I do nil it then my system will have no record of ever invoicing a customer for that item.
The only way I can see of this working well is to have a previous_invoices field that stores the ids of invoices its been on. Doing this would work from an Items point of view Item.previous_invoices would make sense but it would not show up in Invoice.items and to get the items which have been returned I would need to do a db search with a LIKE condition.
I'd rather not use a many to many association as it will only be the tiny fraction of items that are returned which need to use the table.
Is there anyway of doing this in rails or with a gem which can handle the finds on either side?
The relationship between Invoice and Item is many-to-many, and you're just causing yourself difficulty by avoiding implementing it as such. The cleanest solution is an InvoiceItems table with something like a 'returned' field that you would check when you pull invoice items, perhaps with a conditional on the invoice's HABTM association with items.
精彩评论