how to convert legacy query to ActiveRecord Rails way

Posted by josh on Stack Overflow See other posts from Stack Overflow or by josh
Published on 2010-05-03T20:58:48Z Indexed on 2010/05/05 12:08 UTC
Read the original article Hit count: 230

Filed under:
|

I have a query in my code as below

@sqladdpayment = "INSERT INTO payments (orderid, ttlprodcost, paytype, 
paystatus,created_at,updated_at,userid,storeid) 
VALUES ('" + session[:ordersid] + "', '" + session[:totalcost] + "', '"  
+ "1"+ "', '"  + "complete" +"',current_date, current_date, '"+"1"+"','"+   "1"+"')"

Here the table payments and primary key is orderid. Now, I know if I convert this to the ActiveRecord way then I will not have to put update_date, current_date because it will put that on it's own. It will also put orderid on it's own also (auto_increment).

I am looking for a way to convert the above query to ActiveRecord Rails way but still be able to put orderid on my own (session[:ordersid]). I do not want to rely on auto_increment because then I will have to refactor lot of the other code. This might be a quick and dirty fix but I want to know whether this type of flexibility is offered in rails?

I have wondered about this question many times. Why won't rails allow me to have that flexibility?

© Stack Overflow or respective owner

Related posts about rails

Related posts about activerecord