簡體   English   中英

Ransack:通過關聯搜索has_many

[英]Ransack: Search has_many through association

我是Rails的新手,與Ransack有問題:

這是模型項目

 class Project < ApplicationRecord
  searchkick
  belongs_to :company
  belongs_to :m_category
  has_many :project_industries, dependent: :destroy
  has_many :m_industries, through: :project_industries

end

這是模特行業:

 class Industry < ApplicationRecord
  include M
  belongs_to :m_industry_category
  has_many :project_industries, dependent: :destroy, foreign_key: :industry_id
  has_many :projects, through: :project_industries
end

這是模型IndustryCategory:

 class IndustryCategory < ApplicationRecord
  has_many :industries, dependent: :destroy,
    foreign_key: :industry_category_id

  has_many :projects, through: :industries
end

現在,我想通過IndustryCategory搜索Project,但我不知道如何。 請幫我!! TKS

你可以使用這樣的東西

@industrty_category = IndustryCategory.find(params[:id])

@project = @industry_category.projects.all

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM