find_by_sql( ) can also be used to create model objects containing derived column data. If you use the as xxx SQL syntax to give derived columns a name in the result set, this name will be used as the name of the attribute.
items = LineItem.find_by_sql("select *, " + " quantity*unit_price as total_price, " +
" products.title as title " + " from line_items, products " + 
" where line_items.product_id = products.id ")
li = items[0]
puts "#{li.title}: #{li.quantity}x#{li.unit_price} => #{li.total_price}"
评论
发表评论

您还没有登录,请登录后发表评论

hghdo
搜索本博客
最近访客
最近加入圈子
存档
最新评论