Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 0.76 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Create action with a non-persisted record with persisted associations?
  2. class Transaction < ActiveRecord::Base
  3.   # ...
  4.   accepts_nested_attributes_for :user, :shipping_address, :products
  5.   # ...
  6. end
  7.        
  8. class TransactionsController < ApplicationController
  9.   def new
  10.     @transaction = Transaction.new
  11.   end
  12.  
  13.   def create
  14.     @transaction = Transaction.new params[:transaction]
  15.  
  16.     # ...
  17.   end
  18. end
  19.        
  20. class Transaction < ActiveRecord::Base
  21.   has_many :product_transactions
  22.   has_many :products, :through => :product_transactions
  23. end
  24.        
  25. class Product < ActiveRecord::Base
  26.   has_many :product_transactions
  27.   has_many :transactions, :through => :product_transactions
  28. end
  29.        
  30. class ProductTransaction < ActiveRecord::Base
  31.   belongs_to :transaction
  32.   belongs_to :product
  33. end