title: Rails 3 single-table self-reference association doesn't work
body: <p>I need some help.</p>
  
  <p>I'm trying to make a table of posts with a self-referencing association. One Post may have some replies (also Post). I include only the main parts. My Post class is:</p>
  
  <pre><code># == Schema Information
  #
  # Table name: microposts
  #
  #  id          :integer         not null, primary key
  #  content     :string(255)
  #  user_id     :integer
  #  created_at  :datetime
  #  updated_at  :datetime
  #  in_reply_to :integer
  #
  
  class Micropost &lt; ActiveRecord::Base
  
    attr_accessible :content, :in_reply_to
    ...
    belongs_to :user
    # SOMETHING SHOULD BE WRONG HERE ?!?
    has_many :replies, :class_name =&gt; 'Micropost',
                       :inverse_of =&gt; :replied_post
    belongs_to :replied_post, :class_name =&gt; 'Micropost',
                              :foreign_key =&gt; "in_reply_to",
                              :inverse_of =&gt; :replies
  
    ...
  end
  </code></pre>
  
  <p>Obviously, I have also a User class that owns posts.</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    ...
    has_many :microposts, :dependent =&gt; :destroy
    ...
  end
  </code></pre>
  
  <p>While I can create posts and its replies as usual, I cannot use self-associations. For instance, in the next rails console session I create a post and a reply to it from another user:</p>
  
  <pre><code>--- 
  &lt;b&gt;
  1.9.2-p290 :014 &gt; usr1=User.find(1)
  
    User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
   =&gt; #&lt;User id: 1, name: "Marcel Massana", email: "xaxaupua@gmail.com", created_at: "2012-03-13 00:07:12", updated_at: "2012-03-13 00:07:13", encrypted_password: "339f5f93bbbd7d9b4fdc58a89aa74797c23ccd5acb154e3987a...", salt: "849c6fbeb17e1a4a7e3ef3153d8c41f4ff3ca5435915f87f985...", admin: true&gt; 
  
  1.9.2-p290 :020 &gt;   usr2=User.find(2)
  
    User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
   =&gt; #&lt;User id: 2, name: "Pep Massana", email: "fitxia@gmail.com", created_at: "2012-03-13 00:07:13", updated_at: "2012-03-13 00:07:13", encrypted_password: "00707453381192a4cc7a5714df15da0fa4c2ffa944460bc33d2...", salt: "5af002dc3fad187dac26acee522d44d19d6a2daafcec7fb412e...", admin: true&gt; 
  
  1.9.2-p290 :021 &gt; usr1.microposts.create(:content=&gt;"Hi Pep")
  
    SQL (0.7ms)  INSERT INTO "microposts" ("content", "created_at", "in_reply_to", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)  [["content", "Hi Pep"], ["created_at", Wed, 14 Mar 2012 18:08:45 UTC +00:00], ["in_reply_to", nil], ["updated_at", Wed, 14 Mar 2012 18:08:45 UTC +00:00], ["user_id", 1]]
   =&gt; #&lt;Micropost id: 104, content: "Hi Pep", user_id: 1, created_at: "2012-03-14 18:08:45", updated_at: "2012-03-14 18:08:45", in_reply_to: nil&gt; 
  
  1.9.2-p290 :022 &gt; mp1=Micropost.find(104)
  
    Micropost Load (0.2ms)  SELECT "microposts".* FROM "microposts" WHERE "microposts"."id" = ? ORDER BY microposts.created_at DESC LIMIT 1  [["id", 104]]
   =&gt; #&lt;Micropost id: 104, content: "Hi Pep", user_id: 1, created_at: "2012-03-14 18:08:45", updated_at: "2012-03-14 18:08:45", in_reply_to: nil&gt; 
  
  1.9.2-p290 :023 &gt; mp2 = usr2.microposts.create(content: "good to see you Marcel", in_reply_to: 104)
  
    SQL (0.8ms)  INSERT INTO "microposts" ("content", "created_at", "in_reply_to", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)  [["content", "good to see you Marcel"], ["created_at", Wed, 14 Mar 2012 18:11:24 UTC +00:00], ["in_reply_to", 104], ["updated_at", Wed, 14 Mar 2012 18:11:24 UTC +00:00], ["user_id", 2]]
   =&gt; #&lt;Micropost id: 105, content: "good to see you Marcel", user_id: 2, created_at: "2012-03-14 18:11:24", updated_at: "2012-03-14 18:11:24", in_reply_to: 104&gt; 
  &lt;/b&gt;
  
  --- 
  </code></pre>
  
  <p>Then I try to use Post self-associations, but one of them don't work: Post#replied_post seems to work, but Post#replies don't:</p>
  
  <pre><code>--- 
  &lt;b&gt;
  1.9.2-p290 :024 &gt; mp2.replied_post
  
    Micropost Load (0.3ms)  SELECT "microposts".* FROM "microposts" WHERE "microposts"."id" = 104 ORDER BY microposts.created_at DESC LIMIT 1
  
  &lt;Micropost id: 104, content: "Hi Pep", user_id: 1, created_at: "2012-03-14 18:08:45", updated_at: "2012-03-14 18:08:45", in_reply_to: nil&gt; 
  
  1.9.2-p290 :025 &gt; mp1.replies
  
    Micropost Load (0.2ms)  SELECT "microposts".* FROM "microposts" WHERE "microposts"."micropost_id" = 104 ORDER BY microposts.created_at DESC
  SQLite3::SQLException: no such column: microposts.micropost_id: SELECT "microposts".* FROM "microposts"  WHERE "microposts"."micropost_id" = 104 ORDER BY microposts.created_at DESC
  ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: microposts.micropost_id: SELECT "microposts".* FROM "microposts"  WHERE "microposts"."micropost_id" = 104 ORDER BY microposts.created_at DESC
      from /home/marcel/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `initialize'
      from /home/marcel/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `new'
      from /home/marcel/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `prepare'
  ...
  &lt;/b&gt;
  
  --- 
  </code></pre>
  
  <p>Any hint? What's wrong with my association definitions? I've read the documentation ans seems well defined.</p>
  
  <p>TIA</p>
  
  <p>PS: My configuration</p>
  
  <pre><code>About your application's environment
  Ruby version              1.9.2 (i686-linux)
  RubyGems version          1.8.10
  Rack version              1.3
  Rails version             3.1.3
  JavaScript Runtime        therubyracer (V8)
  Active Record version     3.1.3
  Action Pack version       3.1.3
  Active Resource version   3.1.3
  Action Mailer version     3.1.3
  Active Support version    3.1.3
  Middleware                ActionDispatch::Static, Rack::Lock, #&lt;ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x9c4d494&gt;, Rack::Runtime, Rack::MethodOverride, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::RemoteIp, Rack::Sendfile, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, ActionDispatch::Head, Rack::ConditionalGet, Rack::ETag, ActionDispatch::BestStandardsSupport
  Application root          /home/marcel/Dropbox/DESENVOLUPAMENT/Rails3Examples/ror_tutorial
  Environment               development
  Database adapter          sqlite3
  Database schema version   20120313233558
  </code></pre>