has_many:通过,自引用协会
-
20-09-2019 - |
题
我有麻烦的自我引用协会、模型应该给妈一系列模式的left_chunks和right_chunks方法,但是每次我得到一个空阵列
源
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
输出。/脚本/控制台
>> #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
=> []
为什么大块不结在一起吗?
数据库之后,执行代码
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 |
+----+---------------+----------------+---------------------+---------------------+
任何想法?
解决方案
你不说哪个版本的MySQL,红宝石或轨道。我只是想这有一个小测试的应用程序,它工作正常。我使用的PostgreSQL8.4.1在OS X10.6.我刚创建了一个空应用在轨道上2.3.5/红宝石1.8.7(2009-06-12补174)与"轨testapp",然后增加了两个模型在块。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
...和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
...加上两个迁移到加的表格,而没有时间戳,为了简洁起见:
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
...并且:
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
然后我跑"耙db:创建"、"瑞克db:迁移"和你的控制台的命令为我工作如下:
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
=> []
数据库内容之后,上述:
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)
鉴于这样的:
...和这样的:
...我什么都看不到真的错了你的原始代码。也许迁移不是你所期望的,也许还有其它的部份你代码你还没有公布这是干扰(例如过滤器,其他宝石),或者也许Email数据库的适配器MySQL不是在做正确的事情,在这种情况下和/或MySQL不执行不适当的。这是一个有点啰嗦安装PostgreSQL和使用,而不是MySQL为进一步的试验,但我认为这将是值得的。
只要的情况下,它被证明在所有有用的我已经上传测试的应用程序的数据:
如果你找到了什么差错和管理,以纠正,请后续在这里。这将是有用的,如果任何人遭遇的类似麻烦,在未来和读取这一线的同时,寻找一个解决方案。
其他提示
这是一个.reload问题?当您在控制台中做到这一点:
right.left_chunks << left
DO
right.reload
然后尝试
right.left_chunks
。