has_many关联searchkick问题

时间:2020-04-04 17:53:33

标签: ruby elasticsearch elasticsearch-aggregation searchkick

我有与Searchkick一起使用的关联模型模型。 我希望能够进行搜索,以列出List模型中的所有模型信息。 例如 “ frederic 2020-01-01”

我想返回与frederic有关的字段(在本例中为account_name)和与模型发布有关的日期

有人可以帮助我吗? 型号:

   class List < ApplicationRecord
    searchkick 

    has_many :people

end

class Person < ApplicationRecord
  has_many :accounts
  belongs_to :list
  searchkick
end

class Account < ApplicationRecord
  has_many :posts
  belongs_to :person

  searchkick
end


class Post < ApplicationRecord
  belongs_to :account
  searchkick
end

在List_controller上:

class ListsController < ApplicationController
  def index

    if params[:keywords].present?

      @lists = List.search(params[:keywords], operator: "or")
    else 
      @lists = List.all
    end
  end
end

我的schema.rb


  create_table "accounts", force: :cascade do |t|
    t.string "social_network"
    t.string "name_account"
    t.integer "person_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["person_id"], name: "index_accounts_on_person_id"
  end

  create_table "lists", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "people", force: :cascade do |t|
    t.string "name"
    t.integer "list_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["list_id"], name: "index_people_on_list_id"
  end

  create_table "posts", force: :cascade do |t|
    t.string "post_text"
    t.date "date"
    t.string "link"
    t.integer "account_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["account_id"], name: "index_posts_on_account_id"
  end

  add_foreign_key "accounts", "people"
  add_foreign_key "people", "lists"
  add_foreign_key "posts", "accounts"
end

0 个答案:

没有答案