实践了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);
分享到:
相关推荐
CASTLE ACTIVERECORD学习实践(1):快速入门指南 CASTLE ACTIVERECORD学习实践(2):构建配置信息 CASTLE ACTIVERECORD学习实践(3):映射基础 CASTLE ACTIVERECORD学习实践(4):实现ONE-MANY关系的映射 CASTLE...
ActiveRecord简单实例_activerecord.zip
Castle.ActiveRecord For .NET FrameWork 2.0 如果你想使用Castle.ActiveRecord,但又不想使用.NET Framework 3.0/3.5/4.0或更高版本,那么这个就是你所需要的,For .NET FrameWork 2.0,我整理了好久,自己从官方...
将ActiveRecord中的NHibernate升级到3.3.0GA,排除编译的bug问题,保留ActiveRecord的完整功能,【Castle.ActiveRecord 升级NHibernate到3.4.0GA】的功能不完整!
Pro ActiveRecord Databases with Ruby and Rails.pdf
Castle.ActiveRecord官方已经停止更新了,官方最高支持到NHibernate 3.1.0.4000,这个版本还有不少问题(例如:[NH-2213] - CLONE -Wrong parameters order in IQuery with SetParameterList and Filter)。...
简化了ActiveRecord中的first,last,take方法。 Find the first record (or first N records if a parameter is supplied). 旧语法: Person.where(:email => "jenorish@gmail").first 新语法:
MyBatisPlus的ActiveRecord实现CRUD示例代码
userstamp, 这个 Rails 插件扩展ActiveRecord Userstamp插件( v-2.0 )概述Userstamp插件扩展了 ActiveRecord::Base,以添加对'创建者','更新程序'和'deleter'属性的自动更新。 它是基于 ActiveRecord::Timesta
Castle.ActiveRecord 的资料很多,但是WINFORM的没几个,于此我专门写了个例子献给初学Castle.ActiveRecord的童鞋们,希望这篇文档能够帮到你们。这个例子使用的是ACCESS数据库,从单表,一对多,多对多,数据绑定,...
scala-activerecord.zip,scala activerecord scalatraactiverecord类似于scala的orm库
C# Castle.ActiveRecord CS源码示例教程.zip
Castle ActiveRecord快速入门指南,ActiveRecord帮助文档
ORM的中文教程 英文的API 为什么是2分呢? 因为我很2 -_-!
Active Record(活动记录),是一种领域模型模式,特点是一个模型类对应关系型数据库中的一个表,而模型类的一个实例对应表中的一行记录。
ActiveRecord最终版,由于ActiveRecord引用了ISet集合,Iesi.Collections.3.4.1.4000以后已经把ISet去掉了,所以ActiveRecord引用的NHibernate的版本的更新只能到3.4.1.4000此为止。 引用其他资源版本如下: ...
ROR-ActiveRecord持久层框架的Java实现
ActiveRecord的model创建,常用的一些增删改查以及HQL的使用
Apress Pro ActiveRecord Databases with Ruby and Rails <br>Active Record has primarily been covered as a subsection, or maybe as a chapter or two, within a larger scoped book generally about the ...
SchemaPlus 提供增强和扩展ActiveRecord的集合