【问题标题】:Rails with Sunspot add Child Model Attributes带有 Sunspot 的 Rails 添加子模型属性
【发布时间】:2015-05-15 13:56:41
【问题描述】:

所以,我已经搞砸了好几个小时,我想我已经尝试了关于这个问题的所有内容。这是我的第一个问题。

所以,我正在尝试搜索模型(位置):branch_name、:branch_address1 等,并将其子模型(员工):first_name 在该模型中用作其自己的属性。因此,如果位置员工姓名在搜索查询中,则所需的效果将是位置(作为结果)将返回。我试图用这样的代码来实现这一点:

class Location < ActiveRecord::Base
  belongs_to :partner
  has_many :employees, dependent: :destroy

  validates_presence_of :branch_name, :branch_address1, :branch_country, :branch_zip_code
  accepts_nested_attributes_for :employees, allow_destroy: true, :reject_if > proc { |attributes| attributes['first_name'].blank? }

  searchable do
    text :branch_name, :branch_address1, :branch_city, :branch_zip_code
    text :employee_attributes do
      :first_name
      if :employee_attributes
      end
    end
  end

end

我也想使用父模型(合作伙伴)名称,但是我也无法让它工作。

我是否可以在 Locations 表中创建一个新列来捕获每个新员工的姓名,或者我可以在该模型子项中进行搜索吗?

【问题讨论】:

    标签: ruby-on-rails postgresql ruby-on-rails-4 nested-attributes sunspot-rails


    【解决方案1】:

    我使用过带有rails的solr,我也遇到过类似的问题。 就像你问的新专栏一样,我在 solr 中添加了一个新字段

    伪代码

    searchable do
     text :first_name, :stored => true
     string  :partner_name, :stored => true do
       partner.name
     end
    end
    

    请注意,存储字段会增加成本。

    在我的例子中,我不得不触发多个查询,耗时 47 秒,存储值帮助我避免了多个查询,单个查询耗时 5 秒。

    您需要重新索引才能获得结果。

    【讨论】:

    • 当我重新索引时,我仍然会得到这个代码。我的申请中还有什么我应该改变的吗? NoMethodError: undefined method 'first_name' for #&lt;Location:0x007ff308e03528&gt;
    • 我也继续使用这种方法中的想法来尝试类似问题的string :employees_first_name, :stored =&gt; true do employees.first_name end NoMethodError: undefined method 'first_name' for #&lt;Employee
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多