简体   繁体   中英

Ransack: Search has_many through association

I am a newbie in Rails and have issues with Ransack:

This is model Project

 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

This is model Industry:

 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

And this is model IndustryCategory:

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

  has_many :projects, through: :industries
end

Now, I want to search the Project by IndustryCategory but I don't know how. please help me!! tks

You can use something like this

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

@project = @industry_category.projects.all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM