我有两个带有以下关联的模型
class Application < ActiveRecord::Base
belongs_to :registration
end
class Registration < ActiveRecord::Base
has_many :applications, :dependent => :destroy
end
现在我正在尝试进行如下所示的查询
@applications = Application.includes(:registration).where("registration.stdniveau = ? AND uni_id = ? AND offer_sent = ? AND (offer_accepted =? OR offer_accepted =?)", 2, @uni.id, 1, nil, 1).references(:registration)
我也试过这个
@applications = Application.includes(:registrations).where("registrations.stdniveau = ? AND uni_id = ? AND offer_sent = ? AND (offer_accepted =? OR offer_accepted =?)", 2, @uni.id, 1, nil, 1).references(:registrations)
但是这两个查询都给我带来了难以理解的错误。我不明白我在这里错过了什么?
答案 0 :(得分:0)
1)您应该使您的belongs_to单数:注册
2)如果你想根据另一个表进行条件选择,你需要加入它。
3)所以这应该有效:
Application.joins(:registration)
.where("registrations.stdniveau = ? AND uni_id = ? AND offer_sent = ? AND (offer_accepted =? OR offer_accepted =?)", 2, @uni.id, 1, nil, 1")