-2

I recently updated my Rails app from 4.0 to 4.1. Everything seems to work fine, except this one line in my Search Model that was working before.

Essentially, I want to search/find District_Resources by Tag Name and by the District_Resource Name.

**ex.**
If I search the word "Tutoring" 
*I should get all District_Resources with the Resource_Tag "Tutoring"
*And all District Resources that include the word Tutoring in it's Name. 
(i.e Tutoring Services)

For some reason, I keep getting this error:

(Mysql2::Error: Unknown column 'resource_tags.name' in 'where 
clause': SELECT `district_resources`.* FROM `district_resources`  
WHERE (resource_tags.name like '%Tutoring%' OR district_resources.name like '%Tutoring%')  
ORDER BY `district_resources`.`name` ASC):

But that column does exist in the Resource_Tags table.

MODELS

class DistrictResource < ActiveRecord::Base

  has_many :district_mappings, dependent: :destroy
  has_many :resource_tags, through: :district_mappings

  accepts_nested_attributes_for :resource_tags
end

class ResourceTag < ActiveRecord::Base

  has_many :district_mappings, dependent: :destroy
  has_many :district_resources, through: :district_mappings

end

class Search < ActiveRecord::Base

  def district_resources
    @district_resources ||= find_district_resources
  end

  def find_district_resources
    district_resources = DistrictResource.order(:name)

    district_resources = district_resources.includes(:resource_tags).where("resource_tags.name like :name OR district_resources.name like :name", {:name => "%#{name}%" })

    district_resources
  end

end

SCHEMA

create_table "district_resources", force: true do |t|
  t.string   "name"
  t.string   "description"
  t.string   "website"
  t.string   "phone"
  t.string   "email"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "district_mappings", force: true do |t|
  t.integer  "district_resource_id"
  t.integer  "resource_tag_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "resource_tags", force: true do |t|
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
end
Serge Pedroza
  • 2,160
  • 3
  • 28
  • 41

2 Answers2

1

You are referencing district_resources in where clause but this is not joined in query as you are eager loading resource_tags so here are two solutions for this

1.

district_resources = district_resources.joins(:resource_tags).where("resource_tags.name like :name OR district_resources.name like :name", {:name => "%#{name}%" })

2.

district_resources = district_resources.includes(:resource_tags).refereces(resource_tags).where("resource_tags.name like :name OR district_resources.name like :name", {:name => "%#{name}%" })

In both of these cases we are telling rails that we are using resource_tags table in where clause so join district_resources with it

Qaisar Nadeem
  • 2,404
  • 13
  • 23
0

Fixed it!!

district_resources = district_resources.includes(:resource_tags).where("resource_tags.name like :name OR district_resources.name like :name", {:name => "%#{name}%" }).references(:resource_tags)

I had to reference the Resource_Tags Model

Serge Pedroza
  • 2,160
  • 3
  • 28
  • 41