2012-03-14 65 views
0

我需要一些幫助。Rails 3單表自引用關聯不起作用

我正在試圖製作一個帖子與自引用關聯表。一個職位可能有一些答覆(也是郵政)。我只包括主要部分。我的帖子是:

# == 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 < ActiveRecord::Base 

    attr_accessible :content, :in_reply_to 
    ... 
    belongs_to :user 
    # SOMETHING SHOULD BE WRONG HERE ?!? 
    has_many :replies, :class_name => 'Micropost', 
        :inverse_of => :replied_post 
    belongs_to :replied_post, :class_name => 'Micropost', 
          :foreign_key => "in_reply_to", 
          :inverse_of => :replies 

    ... 
end 

很明顯,我也有一個用戶類擁有職位。

class User < ActiveRecord::Base 
    ... 
    has_many :microposts, :dependent => :destroy 
    ... 
end 

儘管我可以像平常一樣創建帖子及其回覆,但我無法使用自我關聯。舉例來說,在未來的軌道控制檯會話創建帖子,並從其他用戶給它一個答覆:

--- 
<b> 
1.9.2-p290 :014 > usr1=User.find(1) 

    User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] 
=> #<User id: 1, name: "Marcel Massana", email: "[email protected]", created_at: "2012-03-13 00:07:12", updated_at: "2012-03-13 00:07:13", encrypted_password: "339f5f93bbbd7d9b4fdc58a89aa74797c23ccd5acb154e3987a...", salt: "849c6fbeb17e1a4a7e3ef3153d8c41f4ff3ca5435915f87f985...", admin: true> 

1.9.2-p290 :020 > usr2=User.find(2) 

    User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]] 
=> #<User id: 2, name: "Pep Massana", email: "[email protected]", created_at: "2012-03-13 00:07:13", updated_at: "2012-03-13 00:07:13", encrypted_password: "00707453381192a4cc7a5714df15da0fa4c2ffa944460bc33d2...", salt: "5af002dc3fad187dac26acee522d44d19d6a2daafcec7fb412e...", admin: true> 

1.9.2-p290 :021 > usr1.microposts.create(:content=>"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]] 
=> #<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> 

1.9.2-p290 :022 > 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]] 
=> #<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> 

1.9.2-p290 :023 > 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]] 
=> #<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> 
</b> 

--- 

然後我嘗試使用後的自我協會,但他們中的一個不工作:帖子# replied_post似乎工作,但郵政編號回覆沒有:

--- 
<b> 
1.9.2-p290 :024 > mp2.replied_post 

    Micropost Load (0.3ms) SELECT "microposts".* FROM "microposts" WHERE "microposts"."id" = 104 ORDER BY microposts.created_at DESC LIMIT 1 

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

1.9.2-p290 :025 > 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' 
... 
</b> 

--- 

任何提示?我的關聯定義有什麼問題?我已經閱讀過文檔,似乎很好的定義。

TIA

PS:我的配置

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, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x9c4d494>, 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 
+0

這不就是一個普通的自聯接嗎?我不認爲你需要:inverse_of位。 http://guides.rubyonrails.org/association_basics.html#self-joins – 2012-03-14 20:34:34

回答

1

我複查了整個思考和發現,我不得不添加:foreign_key => "in_reply_to"也在has_many聲明。

has_many :replies, :class_name => 'Micropost', 
        :foreign_key => "in_reply_to", 
        :inverse_of => :replied_post 
belongs_to :replied_post, :class_name => 'Micropost', 
          :foreign_key => "in_reply_to", 
          :inverse_of => :replies