Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 608707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:29:20+00:00 2026-05-13T17:29:20+00:00

I have trouble with the self referential association, the models should give ma an

  • 0

I have trouble with the self referential association, the models should give ma an array of models for the left_chunks and right_chunks methods, but I get everytime an empty array

The source

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

Why are the chunks not bound together ?

Database after code execution

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

Any ideas ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:29:20+00:00Added an answer on May 13, 2026 at 5:29 pm

    You don’t say what version of MySQL, Ruby or Rails you are on. I just tried this with a small test application and it worked correctly. I’m using PostgreSQL 8.4.1 on OS X 10.6. I just created an empty app on Rails 2.3.5 / Ruby 1.8.7 (2009-06-12 patchlevel 174) with “rails testapp”, then added two models 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
    

    …and 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
    

    …plus two migrations to add the tables, without timestamps for brevity:

    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
    

    …and:

    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
    

    I then ran “rake db:create”, “rake db:migrate” and your console commands worked for me as follows:

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

    The database contents after the above were:

    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)
    

    Given this:

    • http://blog.hasmanythrough.com/2006/4/21/self-referential-through

    …and this:

    • http://blog.hasmanythrough.com/2007/10/30/self-referential-has-many-through

    …I can’t see anything really wrong with your original code. Perhaps the migrations aren’t what you expect, perhaps there are other parts of your code you have not posted which are interfering (e.g. filters, other gems) or perhaps the ActiveRecord database adapter for MySQL isn’t doing the right things in this case and/or MySQL isn’t performing properly. It is a little long-winded to install PostgreSQL and use that instead of MySQL for further tests, but I think it would be worthwhile.

    Just in case it proves at all useful I’ve uploaded the test application data here:

    • http://pond.org.uk/misc/for_messages/chunks_testapp.tar.gz

    If you find out what went wrong and manage to correct it, please post a follow-up here. This will be useful if anyone encounters similar trouble in future and reads this thread while searching for a solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm migrating to Nhibernate 2.0 GA but have some trouble with setting cache expirations
I am developing a small windows app, but have some trouble deciding whether to
Conceptually, I would like to accomplish the following but have had trouble understand how
Conceptually, I would like to accomplish the following but have had trouble understand how
I have trouble comparing 2 double in Excel VBA suppose that I have the
I have trouble with the following piece of code. When I go through with
I have trouble using Perl grep() with a string that may contain chars that
I have trouble defining a trigger for a MySQL database. I want to change
I have no trouble building 1.35.0, as well as 1.36.0 on the timesys arm-gcc
I have being having trouble with the web server used for our intranet. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.