我知道一個非常非常類似的問題已經被問過。如果我想鏈接更多的範圍,那麼對這個問題的解決方案就不起作用,所以我在這裏再次提出問題,並提供更多關於問題來自何處的信息。思維獅身人面像:缺少外鍵的屬性
# Relevant code only...
class Publication < ActiveRecord::Base
has_many :issues
has_many :articles, :through => :issues
end
class Issue < ActiveRecord::Base
belongs_to :publication
has_many :articles
end
class Article < ActiveRecord::Base
belongs_to :issue
define_index do
has issue(:publication_id), :as => :publication_id
end
end
因此,我希望下面的代碼工作:
Publication.first.articles.search 'foobar'
但是,它返回以下錯誤:
RuntimeError: Missing Attribute for Foreign Key publication_id
from /home/matchu/rails/torch/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/active_record/has_many_association.rb:18:in `search'
這似乎暗示publication_id的屬性,我專門設定不存在。但是,它確實如此。
Article.search :with => {:publication_id => 1}
所以,我想我可以使用該語法,即使它明顯不那麼漂亮。提出這個問題並不特別緊急。但我確實很好奇爲什麼會發生這種情況。有什麼想法嗎?
對未來的讀者:帕特解決了這個問題,從而解決了這個問題。謝謝! – Matchu 2009-09-14 19:48:18
我偶然發現了這個尋找解決方案,爲什麼我的索引拋出相同的錯誤;我沒有索引'Client'的關聯,因此在我的索引中添加了'client(:client_id),作爲:: client_id'(因爲上面的問題早已在TS中解決了)。感謝您的指導! – 2013-03-15 14:14:30