Domanda

Ho problemi con l'associazione sé referenziale, i modelli dovrebbe dare ma una serie di modelli per le left_chunks e right_chunks metodi, ma ottengo ogni volta un array vuoto

La fonte

class Chunk < ActiveRecord::Base
  has_many :left_bindings, :foreign_key => "left_chunk_id",
   :class_name => "ChunkChunk",
   :dependent => :destroy
  has_many :right_chunks, :through => :left_bindings
  has_many :right_bindings, :foreign_key => "right_chunk_id",
   :class_name => "ChunkChunk",
   :dependent => :destroy
  has_many :left_chunks, :through => :right_bindings
end

class ChunkChunk < ActiveRecord::Base
 belongs_to :left_chunk, :class_name => "Chunk", :foreign_key => "left_chunk_id"
 belongs_to :right_chunk, :class_name => "Chunk", :foreign_key => "right_chunk_id"
end

Output dal ./script/console

>> #first case
?> 
?> left = Chunk.new({:content => "chunk_one"}); left.save
=> true
>> right = Chunk.new({:content => "chunk_two"}); right.save
=> true
>> left.right_chunks << right
=> []
>> left.right_chunks
=> []
>> left.left_chunks
=> []
>> 
?> #second case
?> 
?> left = Chunk.new({:content => "chunk_three"}); left.save
=> true
>> right = Chunk.new({:content => "chunk_four"}); right.save
=> true
>> right.left_chunks << left
=> []
>> right.left_chunks
=> []
>> right.right_chunks
=> []

Perché i pezzi non legati tra loro?

database dopo l'esecuzione di codice

mysql> select * from chunks;
+----+-------------+---------------------+---------------------+
| id | content     | created_at          | updated_at          |
+----+-------------+---------------------+---------------------+
|  1 | chunk_one   | 2010-02-14 12:11:22 | 2010-02-14 12:11:22 | 
|  2 | chunk_two   | 2010-02-14 12:11:22 | 2010-02-14 12:11:22 | 
|  3 | chunk_three | 2010-02-14 12:11:22 | 2010-02-14 12:11:22 | 
|  4 | chunk_four  | 2010-02-14 12:11:22 | 2010-02-14 12:11:22 | 
+----+-------------+---------------------+---------------------+

mysql> select * from chunk_chunks;
+----+---------------+----------------+---------------------+---------------------+
| id | left_chunk_id | right_chunk_id | created_at          | updated_at          |
+----+---------------+----------------+---------------------+---------------------+
|  1 |          NULL |              2 | 2010-02-14 12:11:22 | 2010-02-14 12:11:22 | 
|  2 |             3 |           NULL | 2010-02-14 12:11:22 | 2010-02-14 12:11:22 | 
+----+---------------+----------------+---------------------+---------------------+

Tutte le idee?

È stato utile?

Soluzione

Non si dice quale versione di MySQL, Ruby o Rails ci si trova. Ho appena provato questo con una piccola applicazione di prova e ha funzionato correttamente. Sto usando PostgreSQL 8.4.1 su OS X 10.6. Ho appena creato un app vuota on Rails 2.3.5 / 1.8.7 Rubino (2009-06-12 patchlevel 174) con "rotaie testapp", ha poi aggiunto due modelli in chunk.rb:

class Chunk < ActiveRecord::Base
  has_many :left_bindings,  :foreign_key => "left_chunk_id",
                            :class_name => "ChunkChunk",
                            :dependent => :destroy
  has_many :right_chunks,   :through => :left_bindings
  has_many :right_bindings, :foreign_key => "right_chunk_id",
                            :class_name => "ChunkChunk",
                            :dependent => :destroy
  has_many :left_chunks,    :through => :right_bindings
end

... e chunk_chunks.rb:

class ChunkChunk < ActiveRecord::Base
 belongs_to :left_chunk,  :class_name => "Chunk", :foreign_key => "left_chunk_id"
 belongs_to :right_chunk, :class_name => "Chunk", :foreign_key => "right_chunk_id"
end

... più due migrazioni per aggiungere le tabelle, senza timestamp per brevità:

class AddChunks < ActiveRecord::Migration
  def self.up
    create_table 'chunks' do | t |
      t.string :content
    end
  end

  def self.down
    drop_table 'chunk'
  end
end

... e:

class AddChunkChunks < ActiveRecord::Migration
  def self.up
    create_table 'chunk_chunks' do | t |
      t.belongs_to :left_chunk
      t.belongs_to :right_chunk
    end
  end

  def self.down
  end
end

Allora ho fatto funzionare "rake db: creare", "rake db: migrate" ei tuoi comandi della console lavorato per me come segue:

PondPro:testapp adh1003$ script/console
Loading development environment (Rails 2.3.5)
>> left = Chunk.new({:content => "chunk_one"}); left.save
=> true
>> right = Chunk.new({:content => "chunk_two"}); right.save
=> true
>> left.right_chunks << right
=> [#<Chunk id: 2, content: "chunk_two">]
>> left.right_chunks
=> [#<Chunk id: 2, content: "chunk_two">]
>> left.left_chunks
=> []
>> left = Chunk.new({:content => "chunk_three"}); left.save
=> true
>> right = Chunk.new({:content => "chunk_four"}); right.save
=> true
>> right.left_chunks << left
=> [#<Chunk id: 3, content: "chunk_three">]
>> right.left_chunks
=> [#<Chunk id: 3, content: "chunk_three">]
>> right.right_chunks
=> []

Il contenuto del database dopo quanto sopra sono stati:

chunk-devel=# SELECT * FROM chunks;
 id |   content   
----+-------------
  1 | chunk_one
  2 | chunk_two
  3 | chunk_three
  4 | chunk_four
(4 rows)

chunk-devel=# SELECT * FROM chunk_chunks;
 id | left_chunk_id | right_chunk_id 
----+---------------+----------------
  1 |             1 |              2
  2 |             3 |              4
(2 rows)

Dato questo:

... e questo:

... non riesco a vedere nulla di veramente sbagliato con il codice originale. Forse le migrazioni non sono quello che vi aspettate, forse ci sono altre parti del codice che hai postato non che sono interferenti (ad esempio filtri, altre gemme) o forse l'adattatore di database ActiveRecord per MySQL non sta facendo le cose giuste in questo caso e / o MySQL non funziona correttamente. E 'un po' prolisso installare PostgreSQL e l'uso che, invece di MySQL per ulteriori test, ma penso che sarebbe utile.

Nel caso in cui si dimostra affatto utile Ho caricato i dati delle applicazioni di test qui:

Se si scopre che cosa è andato storto e riesce a correggerlo, si prega di inviare un follow-up qui. Questo sarà utile se qualcuno incontra problemi simili in futuro e legge questa discussione durante la ricerca di una soluzione.

Altri suggerimenti

E 'questo un problema .reload? Dopo aver fatto questo nella console:

right.left_chunks << left

do

right.reload

quindi provare

right.left_chunks.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top