我正在使用rails。如何在Ruby On Rails中使用內部連接
我需要的是,
@accountUrl = Account.find_by_id(current_account_id)
@details = Detail.find_by_acc_id(@accountUrl.id)
如何編寫內從例如
上述任何一個可以連接查詢。
我正在使用rails。如何在Ruby On Rails中使用內部連接
我需要的是,
@accountUrl = Account.find_by_id(current_account_id)
@details = Detail.find_by_acc_id(@accountUrl.id)
如何編寫內從例如
上述任何一個可以連接查詢。
在這個簡單的情況下,Rails不使用連接時,它加入 「代碼」:
Account.includes(:details).where(:id => current_account_id).first
它會做兩個單獨的查詢。
如果你需要一個條件選擇,你有「手」加入(或通過範圍)
Account.joins(:details).where("details.name" => selected_detail).first
這將使內部聯接,並返回僅佔satistfying的條件。
好吧,我會嘗試吧。謝謝。 –
,U可以寫
has_many :cs, :through => :bs #uses inner join to fetch the records.
退房http://guides.rubyonrails.org/active_record_querying.html和http://asciicasts.com/episodes/215-advanced-queries-in-rails-3
您應該舉例說明您正在嘗試做什麼,您使用的代碼,預期結果以及實際結果或例外情況。 – rewritten
ok.i現在指定 –
添加你的模型的相關部分(在你指定關係的ActiveRecord :: Base子類中的行:belongs_to/has_one等) – rewritten