Search code examples
ruby-on-railsrubyransack

Ransack search has many through


I have a Profile model:

has_one :job_preference, inverse_of: :profile, dependent: :destroy
accepts_nested_attributes_for :job_preference, reject_if: :all_blank, allow_destroy: true

Job Preference model:

belongs_to :profile
has_many :job_preferences_wanted_locations
has_many :wanted_locations, through: :job_preferences_wanted_locations, dependent: :destroy

Job Preference Wanted location model:

class JobPreferencesWantedLocation < ApplicationRecord
  belongs_to :job_preference
  belongs_to :wanted_location

  accepts_nested_attributes_for :wanted_location
end

Wanted locations model:

class WantedLocation < ApplicationRecord
    has_many :job_preferences_wanted_locations
    has_many :job_preferences, through: :job_preferences_wanted_locations, dependent: :destroy
end

Im trying to use Ransack gem in the Profile index to make a query on wanted locations.

I can access these wanted locations like this:

profile.job_preference.wanted_locations.each do |n|
    print n.name  
end

I have read through the Ransack doc and tried variations of:

job_preference_wanted_location_name_cont

but this doesnot seem to work.


Solution

  • try with

    job_preferences_job_preferences_wanted_locations_wanted_location_name_cont
    

    from profile, to associative (job preference - wanted location), to wanted location itself and the name attribute.

    The thing is that if I don't remember bad, ransack doesn't works directly with through associations.