has_many:通过,自我参照联想

时间:2010-02-14 12:34:05

标签: ruby-on-rails associations has-many-through

我在自引用关联方面遇到问题,模型应该为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

./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
=> []

为什么这些块没有捆绑在一起?

代码执行后的

数据库

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 | 
+----+---------------+----------------+---------------------+---------------------+

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

您没有说明您使用的是什么版本的MySQL,Ruby或Rails。我刚用一个小测试应用程序尝试了这个,它运行正常。我在OS X 10.6上使用PostgreSQL 8.4.1。我刚刚使用“rails testapp”在Rails 2.3.5 / Ruby 1.8.7(2009-06-12 patchlevel 174)上创建了一个空的应用程序,然后在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

...和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

然后我运行了“rake db:create”,“rake db:migrate”,你的控制台命令对我有用,如下所示:

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)

鉴于此:

......而且这个:

...我原来的代码看不出任何问题。也许迁移不是你所期望的,也许你的代码的其他部分你没有发布干扰(例如过滤器,其他宝石)或者MySQL的ActiveRecord数据库适配器在这种情况下没有做正确的事情和/或MySQL运行不正常。安装PostgreSQL并使用它代替MySQL进行进一步测试有点啰嗦,但我认为这是值得的。

以防万一有用,我已经在这里上传了测试应用程序数据:

如果您发现问题并设法纠正错误,请在此处发布后续信息。如果将来遇到类似的麻烦并在搜索解决方案时读取此线程,这将非常有用。

答案 1 :(得分:0)

这是一个.reload问题吗?在控制台中执行此操作后:

right.left_chunks << left

right.reload

然后尝试

right.left_chunks