`
like.eagle
  • 浏览: 248607 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ActiveRecord强大的find()方法实践

阅读更多

实践了ActiveRecord的find方法,要实现多张表的联合查询,方法如下:

 

class Product < ActiveRecord::Base
 
  def Product .find_by_ user( user _id)    #相当于self.find_by_user(user_id) ...类级别的函数

 

    Product .find(:all ,
                 :select => "products.name pname, users.name uname, products.position_id, 

                                    products.summary, positions.title ",
                 :from => "products, users, positions ",
                 :conditions => ["products. user _id = user s.id and products.position_id = positions.id and 

                                            user s.id = ?" , user _id],
                 :order       => "products.id DESC ")
  end  

 

end

 

当然还有更直接的方法,那就是ActiveRecord的find_by_sql() 方法。

此方法直接将SQL语句用字符串的形式传进去就行了,举两个例子:


orders = Order.find_by_sql("select * from orders");


orders = Order.find_by_sql("select * from orders where id = ?", order_id);

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics