代码之家  ›  专栏  ›  技术社区  ›  Matchu

thinking sphinx:缺少外键属性

  •  1
  • Matchu  · 技术社区  · 15 年前

    我知道以前有人问过一个非常相似的问题。如果我想在更多的范围内链接,这个问题的黑客解决方案是不起作用的,所以我在这里再次询问,提供更多关于问题来源的信息。

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

    这似乎意味着我专门设置的发布ID属性不存在。然而,它确实如此。

    Article.search :with => {:publication_id => 1}
    

    所以,我想我可以使用这个语法,尽管它明显不那么漂亮。使这个问题不是特别紧急。但我很好奇为什么会这样。有什么想法吗?

    2 回复  |  直到 15 年前
        1
  •  2
  •   pat    15 年前

    我相当肯定(在不陷入代码的情况下——我现在所处的位置已经晚了)你是对的,它应该按照你设置的方式工作。我认为这是一个假设有很多的问题,而不是期望有很多:通过。

    如果你能创造 an issue on GitHub ,这有助于提醒我进一步调查。

    干杯

        2
  •  0
  •   Zef    15 年前

    您没有显示出版物模型中的内容,但您甚至可以调用publication.first.articles吗?思考斯芬克斯不能对关系做任何事情,除非你首先在活动记录中定义它们:

    # in Publication.rb
    has_many :articles, :through => :issues
    

    直接在文章中搜索是可行的,但是仅仅因为你要使sphinx索引成为问题的出版物,它不会在活动记录中创建任何关系。