Hallo Bernd,

Du hast vollkommen recht. Vielen Dank für den Hinweis. Da waren Validations, die ein create verhindert haben.

Viele Grüße

Michael Kastner

Bernd Stegelmann schrieb:
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


------------------------------------------------------------------------

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

--

------------------------------
galt.de Web :-) Services
Michael Kastner
Berliner Straße 3
D-65439 Flörsheim

Telefon +49 (0) 6145 / 938 581
Telefax +49 (0) 6145 / 938 582
E-Mail [EMAIL PROTECTED]
Homepage http://www.galt.de
------------------------------
_______________________________________________
rubyonrails-ug mailing list
[email protected]
http://mailman.headflash.com/mailman/listinfo/rubyonrails-ug

Antwort per Email an