Hello Mike,
Sorry for the delay: I had to pull my last hair on this problem ;-)
I have tried to recreate a simpler case and here it is.
Say a company wants to keep track of invoices they issue in a DB. Each
invoice will include a list of the products sold.
==============================================
Here is the DB schema:
ActiveRecord::Schema.define(:version => 20090213144943) do
create_table "invoices", :force => true do |t|
t.string "customer"
t.string "invoice_ref"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "products", :force => true do |t|
t.string "reference"
t.integer "invoice_id"
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
end
end
============================
The two models:
class Invoice < ActiveRecord::Base
has_many :products
end
class Product < ActiveRecord::Base
belongs_to :invoice
end
=======================================
My controllers
class InvoiceController < ApplicationController
def index
@invoices = Invoice.find :all
end
def new
@invoice = Invoice.new(params[:invoice])
if request.post?
case params[:commit]
when 'Create'
@invoice.save
when 'Cancel'
redirect_to :action => 'index'
end
end
end
end
class ProductController < ApplicationController
protect_from_forgery :only => [:create, :update, :delete]
active_scaffold :product
end
=======================================================
And the invoice/new view
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/
html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>Create New Invoice</title>
</head>
<body>
<% form_for :invoice do |f| %>
<p>Invoice Nr.<br />
<%= f.text_field :invoice_ref %></p>
<p>Customer Name<br />
<%= f.text_field :customer %></p>
<%= render :active_scaffold => 'product', :label =>
'Products', :constraints => { :invoice_id => @invoice.id } -%>
<p><%= submit_tag 'Create' %><%= submit_tag 'Cancel' %></p>
<% end %>
</body>
</html>
I think this gives the full picture but let me know if you need more
details.
Basically, when I create a new invoice and add products to it at the
same time,
the products don't receive any invoice_id and become orphans in my DB.
Consequently, that invoice has no product list.
So if before_create_save is the solution, could you explain how I
should implement it?
Thanks in advance for your help.
Christian.
On Feb 17, 3:26 pm, Mike Gaffney <[email protected]> wrote:
> Christian,
> You deleted the context of your problem so I'm not sure. Could you
> put the base problem in again?
>
> -Mike
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"ActiveScaffold : Ruby on Rails plugin" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/activescaffold?hl=en
-~----------~----~----~----~------~----~------~--~---