Hallo Michael,

ich vermute, dass die Invoice i beim create aus irgendeinem Grund
(Validations wahrscheinlich) nicht gespeichert wird. In diesem Fall gibt
create dennoch das ungespeicherte Objekt zurück, das aber dann keine ID hat.
Solange i aber keine ID hat kann Rails natürlich die Verknüpfungstabelle
nicht ausfüllen (dafür braucht man ja beide IDs).
Ich an Deiner Stelle würde nach dem create mal i.valid? und i.id.nil?
prüfen um sicherzustellen, dass das Speichern geklappt hat.

Sonnigen Gruß,
Bernd

Michael Kastner schrieb am 22.05.2007 09:23:
> Hallo,
> 
> ich versuche gerade eine has_many through-Assoziation zu testen, aber
> ich erhalte eine Fehlermeldung:
> 
> 1) Error:
> test_add_products(InvoiceTest):
> ActiveRecord::HasManyThroughCantAssociateNewRecords: Cannot associate
> new records through 'Invoice#sales' on '#'. Both records must have an id
> in order to create the has_many :through record associating them.
> 
> /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/has_many_through_association.rb:52:in
> `<<'
>     test/unit/invoice_test.rb:16:in `test_add_products'
> 
> 
> Ich habe folgende Verknüpfung
> 
> invoices => sales <= products
> 
> sales ist also die Kreuztabelle.
> 
> Mein Test sieht so aus:
> 
> require File.dirname(__FILE__) + '/../test_helper'
> 
> class InvoiceTest < Test::Unit::TestCase
>   fixtures :users
>   fixtures :products
> 
>   def test_add_products
>     i = Invoice.create :user => users(:thatsme)
>     i.products << products(:first_product)
>   end
> end
> 
> Habe ich etwas übersehen?
> 
> Viele Grüße
> 
> Michael Kastner
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> rubyonrails-ug mailing list
> [email protected]
> http://mailman.headflash.com/mailman/listinfo/rubyonrails-ug

-- 
********************************

Bernd Stegelmann,
       Senior Software Engineer

Mobil     0177- 670 670 6
E-Mail    [EMAIL PROTECTED]

********************************

CAPITA Unternehmensberatung GmbH
Farnstraße 38
22335 Hamburg

Telefon 040 - 59 27 76
Telefax 040 - 50 61 63
E-Mail  [EMAIL PROTECTED]

Amtsgericht Hamburg, HRB 72014
Geschäftsführer: Christian Sage
USt-ID DE 204 147 342

********************************
_______________________________________________
rubyonrails-ug mailing list
[email protected]
http://mailman.headflash.com/mailman/listinfo/rubyonrails-ug

Antwort per Email an