ROR学习笔记06.12.24-26

=========
06.12.24
=========
多字段组合查询只有 _and_ 这样一种形式;不能在字段名之间用 _or_ 分隔。

除了 save() 方法之外,还可以用 update_attribute() 和 update() 方法来更新某几个特定字段的值

=========
06.12.26
=========
一对一关联:has_one:单数 ; belongs_to:单数
一对多关联:has_many:复数 ; belongs_to:单数
多对多关联:has_and_belongs_to_many:复数 ; has_and_belongs_to_many:复数
-------------------------------------------
belongs_to :父类
           :class_name => "" #"Order"
           :foreign_key => "" #"order_id"
           :conditions => "" #"paid_on is not null"
           :dependent => "" #字表中纪录不能独立于父表中的纪录而存在
           :order => "" #判断如何对多条纪录排序
has_one :the same as belongs_to
--------------------------------------------
乐观锁:
1.在表中建立 lock_version 字段,默认为 0 ,乐观锁就建立了;
2.禁用乐观锁:ActiveRecord::Base.lock_optimistically = false
---------------------------------------------
计数器:计数器缓存 counter caching
1.在子模型对象的 belongs_to 声明中加上 :counter_cache 选项
2.在父表的定义中加上 "字表名称加上 _count 后缀" 字段,初始值必须为 0
----------------------------------------------
MYSQL使用InnoDB存储引擎才支持事务

事务:
begin
    modelname.transaction(模型对象) do
      ...
    end
rescue
    ...
end
-----------------------------------------
ActiveRecord补充

Acts As List:
acts_as_list :scope => :parent_id #指定该列表是针对每个父对象单独建立的
move_highter();move_lower();move_to_bottom();move_to_top()  #这些方法定位
higher_item();lower_item() #前一个,后一个
first?() ; last?()  #是否位于列表的前端或尾端

Acts As Tree
acts_as_tree :order => "name"    #:order表示:在查找某个特定节点的子节点时,按照子节点的name字段排序
--------------------
字符串处理:
数组.compact.join(" ")    #将数组中的字符串以" "连接
String.split(/,/)         #将一个字符串分割为子字符串,然后将结果作为字符串数组返回
-----------------------------
Aggregation聚合
用于保存复合数据的类(譬如这里的Name类)必须符合两个条件:
1.必须有一个构造函数initialize(),其参数必须对应于它所映射的数据库字段;
2.必须为所映射的每个字段提供一个可读的属性  #attr_reader
class Name
  attr_reader :first,:initials,:last
  def initialize (first,initials,last)
    @first = first
    @initials = initials
    @last = last
  end
  def to_s            #以字符串的形式输出客户全名
    [ @first, @initials, @last ].compact.join(" ")
  end
end

class Customer < ActiveRecord::Base
  composed_of :name,               #把数据库字段映射到Name对象上
              :class_name => Name
              :mapping =>
                [
                  [ :first_name, :first ] ,
                  [ :initials, :initials ],
                  [ :last_name, :last ]
                ]
end
------------------------------------
单表继承
继承体系中的类被映射到同一张表中!
父表中:1.type字段用来保存当前纪录对应的对象属于什么类型——按照ActiveRecord的约定;
        2.除非一个字段是所有子类都拥有的,否则就必须让它允许null值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值