0

My clients has_many regions, and my regions has_many clients. Through the join table Regionmemberships.

I want to filter through my clients and find the region_id's that I have clicked in my form. I can get it to work in my console, but not in my app.

The following code works when I try to manually filter through them in my console. I want a similar or same method in my find_results method in my search.rb model, but it seems as if :regionmemberships is not recognized as connected to my clients.

The console code that finds what I want:

results = Client.all
results = results.where(category_id: 3)
results = results.includes(:regionmemberships).where(“regionmemberships.region_id” => “1”)

But if I try the same in my app, it throws me this error:

undefined local variable or method `“regionmemberships' for #<Search:0x007ffdd44e96c8>

And refers to this error: (:regionmemberships is marked in the error-text)

def find_results
    results = Client.order(:name) 
    results = results.where(visible: true)
    results = results.where(category_id: category_id) if category_id.present?
    results = results.includes(:regionmemberships).where(“regionmemberships.region_id” => “0”)   

This is how my app looks:

Models:

Client.rb
has_many :regionmemberships, :dependent => :destroy
has_many :regions, through: :regionmemberships
belongs_to :category

Region.rb
has_many :regionmemberships, :dependent => :destroy
has_many :clients, through: :regionmemberships

Regionmembership.rb
belongs_to :client
belongs_to :region

Category.rb
has_many :clients

The relevant form in my new.html.erb view

new.html.erb
<%= f.collection_select :region_id,
Region.all, :id, :name, {include_blank: false}, 
{class: "regionclass", size: "1"} %>

Searches_controller

  def new
    @search = Search.new

  def create
    @search = Search.create!(search_params)
    redirect_to @search 
  end

  def show
    @search = Search.find(params[:id])
  end

def search_params
    params.require(:search).permit(:category_id, :region, :regions, :regionmembership, :regionmemberships, :visible, :region_id, :pricerange1, :pricerange2, :pricerange3, :pricerange4, :pricerange5, region_ids: [])
end

Search.rb (This is where I need to create a similar code to my console-example - But it seems like :regionmemberships doesnt exist / can't be found / referred to)

class Search < ApplicationRecord
    def results
        @results ||= find_results
    end

private
    def find_results
        results = Client.order(:name) # Her skal ændres til position hvis visse clients ska
        results = results.where(visible: true)
        results = results.where(category_id: category_id) if category_id.present?
        results = results.includes(:regionmemberships).where(“regionmemberships.region_id” => “1”)    
    end
end

Hope anyone can help! It seems as if I need the app to understand that a client is connected to a :regionmemberships column. Thanks!!

sneglefar
  • 117
  • 9
  • `includes` is for eager loading - Rails may decide to do that in two separate queries, in which case your first query has no join to the association. Try to force it with `joins(:regionmemberships)`, or chaining `.references(:regionsmemberships)`. – Thilo Sep 02 '16 at 12:15
  • Thanks! Not sure if I understand where I should change it to joins or references. Can you help me out with an example? Very much appreciated! – sneglefar Sep 02 '16 at 13:01
  • Try `results.joins(:regionmemberships).where(“regionmemberships.region_id” => “1”` – Thilo Sep 02 '16 at 19:35

1 Answers1

0

This code did it!

results = results.joins(:regionmemberships).where('regionmemberships.region_id = ?', region_id)  

With help from this thread: Rails joins query

Community
  • 1
  • 1
sneglefar
  • 117
  • 9