Rails3 Active record complex where clause

When I do such a query, AR misses the second 1/2 of the where clause (assigned_product_id):

XpRawBillDetailProduct.joins(:xp_raw_bill_detail).where(“xp_raw_bill_details.account_subcode_id=#{account_subcode.id}”, :assigned_product_id => raw_bill_product.assigned_product_id).to_sql

“SELECT "xp_raw_bill_detail_products".* FROM "xp_raw_bill_detail_products" INNER JOIN "xp_raw_bill_details" ON "xp_raw_bill_details"."id" = "xp_raw_bill_detail_products"."xp_raw_bill_detail_id" WHERE (xp_raw_bill_details.account_subcode_id=11)”

I have found I can get around this by using two wheres:

(rdb:1) XpRawBillDetailProduct.joins(:xp_raw_bill_detail).where(“xp_raw_bill_details.account_subcode_id=#{account_subcode.id}”).where(:assigned_product_id => raw_bill_product.assigned_product_id).to_sql “SELECT "xp_raw_bill_detail_products".* FROM "xp_raw_bill_detail_products" INNER JOIN "xp_raw_bill_details" ON "xp_raw_bill_details"."id" = "xp_raw_bill_detail_products"."xp_raw_bill_detail_id" WHERE (xp_raw_bill_details.account_subcode_id=11) AND ("xp_raw_bill_detail_products"."assigned_product_id" = 107)” (rdb:1)

But is there a prettier way to mix inside a single where? Seems wrong the way I am doing it.

If you do this, AR assumes that the hash you provided is a map for substituting named bind variables (ie if your query contained " AND assigned_product_id = :assigned_product_id" it would replace :assigned_product_id with the value from the hash

Fred

When I do such a query, AR misses the second 1/2 of the where clause

(assigned_product_id):

XpRawBillDetailProduct.joins(:xp_raw_bill_detail).where(“xp_raw_bill_detail s.account_subcode_id=#{ account_subcode.id}”, :assigned_product_id =>

raw_bill_product.assigned_product_id).to_sql

If you do this, AR assumes that the hash you provided is a map for

substituting named bind variables (ie if your query contained " AND

assigned_product_id = :assigned_product_id" it would

replace :assigned_product_id with the value from the hash

Thanks Fred, makes sense.