简体   繁体   中英

SQL Query in two different Rails models that have no association

I have a landlords table, and landlord_addresses table, and a landlord_companies table. From the landlords index view and using the landlords_controller I need to be able to search the landlord_companies, BUT landlords and landlord_companies have no ActiveRecord association with each other. Obviously I can't use what I've written below, but I am not sure how to search the landlord_companies ...any help would be great!

@landlord = @landlords.includes(:landlord_company)
   .references(:landlord_companies)
   .where("landlord_companies.llc_name LIKE ?", "%#{params[:landlord_llc_search]}%")

Schema:

create_table "landlords", force: :cascade do |t|
   t.string   "name"
   t.string   "contact_name"
   t.string   "contact_number"
   t.integer  "listing_agent_id"
   t.boolean  "own_application"
   t.boolean  "own_credit"
   t.boolean  "accepts_roommate_matchups"
   t.boolean  "management_company"
   t.datetime "created_at",                null: false
   t.datetime "updated_at",                null: false
end

create_table "landlord_companies", force: :cascade do |t|
   t.string   "llc_name"
   t.datetime "created_at",          null: false
   t.datetime "updated_at",          null: false
   t.integer  "landlord_address_id"
end

I was able to figure this out and actually use the SQL query I had already written. I thought there was no association, but technically there was using a through.

Landlord.rb

has_many :landlord_companies, through: :landlord_addresses

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