This question concerns three models:
Sale
class Sale < ActiveRecord::Base
has_many :sale_items
has_many :items, through :sale_items
end
Item
class Item < ActiveRecord::Base
has_many :sale_items
has_many :sales, :through => :sale_items
end
SaleItem
class SaleItem < ActiveRecord::Base
belongs_to :sale
belongs_to :item
end
To explain, an item acts as a base template for a sale_item. The application has many Items, but these are not necessarily a part of every Sale. So, sale_item.name actually points to sale_item.item.name, and sale_item's price method looks like this:
def price
super || item.price
end
A sale_item either gets its price from its item, or that price can be overridden for that specific sale_item by editing its price column in the database.
This is what I'm having difficulty with in my sales/_form.html.erb view: I essentially need a table of all Item objects that looks the table in this Tinkerbin: http://tinkerbin.com/46T7JAKs.
So, what that means is that if an unchecked checkbox gets checked and the form is submitted, a new SaleItem needs to be created with an item_id equal to that if the Item from the list, and with appropriate price and quantity fields (quantity is specific to SaleItem and does not exist for Item objects).
Additionally, if the Sale that is being edited already includes a specific SaleItem, that checkbox should already be checked when the form view is rendered (so unchecking a box for a row would delete the SaleItem object associated with that Item and this Sale).
I'm not sure how this could be done—maybe I'm doing it wrong from the beginning. I toyed with the idea of doing away with the SaleItem model altogether and just creating a items_sales table with the fields sale_id, item_id, price, and quantity, but I'm not sure that is the best pattern.
Update
The previous solution I posted ended up with some flaws and failing tests. I finally figured it out, but will post the real solution shortly.
SaleItem, and this is what I can't figure out how to do. I'm not sure how to build the view.