2016-10-21 32 views
0

私は家主テーブルとlandlord_addressesテーブルとlandlord_companiesテーブルを持っています。家主のインデックスビューとlandlords_controllerを使用して、私はhouselord_companiesを検索する必要がありますが、家主とlandlord_companiesにはActiveRecordの関連付けがありません。明らかに私は私が以下に書いたものを使うことはできませんが、私はどのように検索するかわかりませんlandlord_companies ...どんな助けも素晴らしいでしょう!関連がない2つの異なるRailsモデルのSQLクエリ

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

スキーマ:私はこれを理解し、実際に私はすでに書かれていたSQLクエリを使用することができました

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 
+0

あなたが家主の企業を検索したい条件に基づいて? – uzaif

+0

@uzaif検索する必要があるllc_name列が、landlord_companiesテーブルにあります。 –

+0

と、家主と家主の間に関連はありませんか? – uzaif

答えて

1

。私は関連がないと思ったが、技術的にスルーを使用していた。

Landlord.rb

has_many :landlord_companies, through: :landlord_addresses 
+0

偉大な仕事の男... – uzaif